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.

Roland
  • 127,288
  • 10
  • 191
  • 288
REnthusiast
  • 1,591
  • 3
  • 16
  • 18

1 Answers1

0

Some data:

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

Sounds like you want something like:

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

Or maybe:

t(sapply(data,FUN=function(x) cbind(table(x),prop.table(table(x)))))
Thomas
  • 43,637
  • 12
  • 109
  • 140