0

How can i manipulate this code using perhaps a for-loop?

tbl1 <- table(Column1Name)

cbind(tbl1,prop.table(tbl1))

This code works well for one column but there is over 100 columns in the data set that I am working with at the moment so it is very inefficient for me to repeat this and change the column name etc. each time.

Any help would be greatly appreciated.

tbl1 <- table(Column1Name)

cbind(tbl1,prop.table(tbl1))

gives the count and percentage of each of the factors in each variable and I want to do this for all of my variables using a general code for all.

4

1 回答 1

0

一些数据:

data <- as.data.frame(matrix(sample(1:5,1000,replace=TRUE),nrow=100))

听起来你想要这样的东西:

lapply(data,FUN=function(x) rbind(table(x),prop.table(table(x))))

或者可能:

t(sapply(data,FUN=function(x) cbind(table(x),prop.table(table(x)))))
于 2013-06-13T08:46:07.317 回答