Take the 2-minute tour ×

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!

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

You could use tapply:

tapply(dat[,1], dat[,2], as.vector)

[1] "sample1" "sample3"

[1] "sample2"

EDIT: If your rownames matter, you can use identity as the function:

tapply(dat[,1], dat[,2], identity)

        a         c 
"sample1" "sample3" 

share|improve this answer
thank you!! Worked like a charm. –  pepsimax Oct 2 '12 at 8:52
add comment

Your Answer


