Reputation: 2412
I have a data.frame that looks like
a = c("sample1", "asd")
b = c("sample2" ,"poua")
c = c("sample3", "asd")
dat <- rbind(a,b,c)
I would like to count column2 string frequencies, and keep column1 name in list/ragged array of counts. i.e. have it look like
asd sample1 sample3
poua sample2
I know table counts frequency, but I wasnt able to get it to keep names, so I would really really appreciate any help!
Upvotes: 2
Views: 732
Reputation: 15415
You could use tapply
:
tapply(dat[,1], dat[,2], as.vector)
$asd
[1] "sample1" "sample3"
$poua
[1] "sample2"
EDIT: If your rownames matter, you can use identity
as the function:
tapply(dat[,1], dat[,2], identity)
$asd
a c
"sample1" "sample3"
$poua
b
"sample2"
Upvotes: 3