r - joining strings in a table according to values in another column -


this question has answer here:

i got table this:

id     words 1     school. 2     hate school. 3     cakes. 1     cats. 

here's want do, joining strings in each row according id.

id    words 1    school. cats. 2    hate school. 3    cakes. 

is there package in r?

we can paste 'words' grouped 'id'. can done of group operations. 1 way data.table. convert 'data.frame' 'data.table' (setdt(df1)) , operation mentioned above.

# install.packages(c("data.table"), dependencies = true) library(data.table) setdt(df1)[, list(words = paste(words, collapse=' ')), = id] 

a base r operation use aggregate

aggregate(words~id, df1, fun= paste, collape=' ') 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -