2016-03-28 97 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關於這個話題。

相關問題