简体   繁体   中英

R: Creating a Table of Factor Levels

I am working with the R programming language. Suppose I have the following data:

var1 <- c("A1", "A2", "A3")
var2 <- c("B1", "B2" )

var_1_a <- as.factor(sample(var1, 100, replace=TRUE, prob=c(0.3, 0.3, 0.4)))

var_1_b <- as.factor(sample(var2, 100, replace=TRUE, prob=c(0.5, 0.5)))

my_data = data.frame(var_1_a, var_1_b)

head(my_data)

  var_1_a var_1_b
1      A2      B1
2      A2      B2
3      A1      B2
4      A2      B2
5      A2      B2
6      A3      B1

I am trying to create a new data set that summarizes the old data set, something like this:

new_data = data.frame(var_1_a = table(my_data$var_1_a ), var_2_a = table(my_data$var_1_b ) )

But this results in the following error:

Error in data.frame(var_1_a = table(my_data$var_1_a), var_2_a = table(my_data$var_1_b)) : 
  arguments imply differing number of rows: 3, 2

Can someone please tell me what I am doing wrong?

Thanks!

Try making a list instead of a data frame that will allow for different number of rows.

new_data = list(var_1_a = data.frame(table(my_data$var_1_a )), var_2_a = data.frame(table(my_data$var_1_b)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM