I'm not sure I would use any apply statements here, but the reshape2
package would help.
#sample data
dd<-data.frame(
ID=c(1,2,3,4,5,6),
T1=c(1,2,2,1,1,2),
T2=c(1,1,2,1,1,2),
T3=c(2,1,1,2,1,1)
)
library(reshape2)
mm<-melt(dd,id.vars="ID", variable.name="Measure", value.name="FactorID")
#option 1 (useful for counts of discrete values)
as.data.frame(with(mm, table(FactorID, Measure))
#option 2 (useful for collapsing data)
aggregate(ID~FactorID+Measure, mm, FUN=length)
I used standard base functions for collapsing the data and making counts. I tend to perfer the syntax of reshape2
to the base reshape()
function but that might be able to work as well.