2016-03-28 125 views
1

我在R中的以下小例子:获取的多个变量/列分类因子数中的R

testing = data.frame(c("Once a week", "Once a week", "Rarely", "Once a month", "Once a month"), c("Once a month", "Once a month", "Once a week", "Rarely", "Rarely")) 
colnames(testing) = c("one", "two") 
testing 

     one   two 
1 Once a week Once a month 
2 Once a week Once a month 
3  Rarely Once a week 
4 Once a month  Rarely 
5 Once a month  Rarely 

我想最终的结果是具有所有可能的绝对因素,一列的数据帧而列的其余部分是这样的每列/变量计数:

categories one two 
Rarely  1  2 
Once a month 2  2 
Once a week 2  1 

我有R上的库没有任何限制所以无论将是最容易在这里(也许plyr/dplyr?)。

谢谢。

回答

7

表的工作原理,无需外部的包:

sapply(testing, table) 
#    one two 
#Once a month 2 2 
#Once a week 2 1 
#Rarely   1 2 
+0

OP请求data.frame,而这看起来像一个矩阵。可能想要胁迫什么的。 – Frank

+1

@弗兰克同意。 OP可以把它变成任何有用的格式 –

+1

谢谢,我只是在它周围扔一个'as.data.frame'。 :) – firefly2442

2

您可以用tidyrdplyr包整理好您的桌子和计数与基地table功能

testing = data.frame(c("Once a week", "Once a week", "Rarely", "Once a month", "Once a month"), c("Once a month", "Once a month", "Once a week", "Rarely", "Rarely")) 
colnames(testing) = c("one", "two") 
testing 
#>   one   two 
#> 1 Once a week Once a month 
#> 2 Once a week Once a month 
#> 3  Rarely Once a week 
#> 4 Once a month  Rarely 
#> 5 Once a month  Rarely 

library(tidyr) 
library(dplyr) 

testing %>% 
    gather("type", "categories") %>% 
    table() 
#>  categories 
#> type Once a month Once a week Rarely 
#> one   2   2  1 
#> two   2   1  2 

# or reorder colum before table 
testing %>% 
    gather("type", "categories") %>% 
    select(categories, type) %>% 
    table() 
#>    type 
#> categories  one two 
#> Once a month 2 2 
#> Once a week 2 1 
#> Rarely   1 2 
1

这里类别真实使用tidyr::gather另一种方式,tidyr::spreaddplyr::count

library(dplyr) 
library(tidyr) 

testing %>% 
    gather(measure, value) %>% 
    count(measure, value) %>% 
    spread(measure, n) 

# Source: local data frame [3 x 3] 
# 
#   value one two 
#   (chr) (int) (int) 
# 1 Once a month  2  2 
# 2 Once a week  2  1 
# 3  Rarely  1  2 

而且,看到这fantastic gist关于这个话题。