Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
2  
Please include a small reproducible example with some data. – SimonO101 13 hours ago

1 Answer

up vote 0 down vote accepted

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)))))
share|improve this answer
lapply(data,FUN=function(x) rbind(table(x),prop.table(table(x)))) worked perfectly! Thanks a million :) – Denis 13 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.