Hi, I have a following situation with data: col1 col2 mir-1 aaa mir-1 abc mir-1 aac mir-2 abb mir-2 aaa mir-3 cag mir-3 ccc mir-3 aaa I want to have output in this format: col1 col2 mir-1 aaa, abc, aac mir-2 abb, aaa mir-3 cag,ccc,aaa I have sorted column 1 so that the grouping could be possible. I could have done the same with mysql using a simple query, but I am not sure how I can do it in R. Please suggest me possible way. Thanks, Joshi
Gabor Grothendieck
2008-Nov-30 18:19 UTC
[R] grouping similar column values into a single row
See ?tapply> tapply(paste(DF$col2), DF$col1, c)$`mir-1` [1] "aaa" "abc" "aac" $`mir-2` [1] "abb" "aaa" $`mir-3` [1] "cag" "ccc" "aaa"> # or > t(t(tapply(paste(DF$col2), DF$col1, paste, collapse = " ")))[,1] mir-1 "aaa abc aac" mir-2 "abb aaa" mir-3 "cag ccc aaa"> # or > t(do.call(cbind, tapply(paste(DF$col2), DF$col1, ts)))[,1] [,2] [,3] mir-1 "aaa" "abc" "aac" mir-2 "abb" "aaa" NA mir-3 "cag" "ccc" "aaa" On Sun, Nov 30, 2008 at 1:08 PM, T Joshi <tejalonline at gmail.com> wrote:> Hi, > I have a following situation with data: > col1 col2 > mir-1 aaa > mir-1 abc > mir-1 aac > mir-2 abb > mir-2 aaa > mir-3 cag > mir-3 ccc > mir-3 aaa > > I want to have output in this format: > col1 col2 > mir-1 aaa, abc, aac > mir-2 abb, aaa > mir-3 cag,ccc,aaa > > > I have sorted column 1 so that the grouping could be possible. I could > have done the same with mysql using a simple query, but I am not sure > how I can do it in R. Please suggest me possible way. > > Thanks, > Joshi > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >