2016-03-23 8 views
0

wideデータセットの値に数えてを加算すると、のデータセットをlong(Person-Period)ファイルに変換する必要がありません。データが溶けずにRカウントが発生する

私のデータは次のようになります。私は何をしたいか

 alone1 alone2  alone3  alone4  alone5  alone6  alone7  alone8 
1 Mentioned Mentioned  Mentioned  Mentioned  Mentioned  Mentioned  Mentioned  Mentioned 
2 Mentioned Mentioned  Mentioned  Mentioned  Mentioned  Mentioned  Mentioned  Mentioned 
3 Mentioned Mentioned Not mentioned Not mentioned Not mentioned Not mentioned Not mentioned Not mentioned 
4 Mentioned Mentioned Not mentioned Not mentioned Not mentioned Not mentioned Not mentioned Not mentioned 

が私を与えるこの

library(dplyr) 
library(tidyr) 

dt %>% gather %>% group_by(value) %>% summarise(n = n()) 

である(これは私が欲しい出力です)

  value n 
1  Mentioned 20 
2 Not mentioned 12 

しかし、私はしたくないですmeltまたはgather私データ。

人周期変換を行わずに列の出現をどのように数えることができますか?

dt = structure(list(alone1 = c("Mentioned", "Mentioned", "Mentioned", 
"Mentioned"), alone2 = c("Mentioned", "Mentioned", "Mentioned", 
"Mentioned"), alone3 = c("Mentioned", "Mentioned", "Not mentioned", 
"Not mentioned"), alone4 = c("Mentioned", "Mentioned", "Not mentioned", 
"Not mentioned"), alone5 = c("Mentioned", "Mentioned", "Not mentioned", 
"Not mentioned"), alone6 = c("Mentioned", "Mentioned", "Not mentioned", 
"Not mentioned"), alone7 = c("Mentioned", "Mentioned", "Not mentioned", 
"Not mentioned"), alone8 = c("Mentioned", "Mentioned", "Not mentioned", 
"Not mentioned")), .Names = c("alone1", "alone2", "alone3", "alone4", 
"alone5", "alone6", "alone7", "alone8"), class = "data.frame", row.names = c(NA, 4L)) 

答えて

2

それはあなたが本当にちょうどあなたが使用することができますカウントtableunlistあなたdata.frame

table(unlist(dt)) 

Mentioned Not mentioned 
     20   12 
+0

おかげで素敵なシンプルなソリューションを必要とします – giacomo

関連する問題