R - 计算没有融化数据的事件

时间:2016-03-23 18:27:52

标签: r count sum

我正在尝试简单地计算className数据集的值,但没有必须将数据集转换为wide(人 - 期)文件。

我的数据如下:

long

我想做的是这个

     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

1 个答案:

答案 0 :(得分:2)

你真的需要你可以使用tableunlist data.frame

的计数
table(unlist(dt))

Mentioned Not mentioned 
       20            12