1

我有一个数据框df和我感兴趣的特定列myCol

df <- data.frame("Col1"=c("yes", "yes", "no"), "Col2"=c("no", "no", "yes"), "Col3"=c("no", "yes", "no"), "Col4"=c("yes", "yes", "yes"))

df
  Col1 Col2 Col3 Col4
1  yes   no   no  yes
2  yes   no  yes  yes
3   no  yes   no  yes

myCol <- c("Col1", "Col2", "Col4")

我正在尝试仅计算(按列)列数no和列yesmyCol

我尝试使用summarise, counttable但它要么返回错误,要么返回嵌套计数。

谢谢你的帮助 !

编辑

为了澄清起见,我的输出可能如下所示:

    Col1  Col2  Col4
no     1     2     0
yes    2     1     3
4

2 回答 2

1

一个dplyr选项purrr可能是:

map_dfc(.x = c("yes", "no"),
        ~ df %>%
         transmute(!!.x := rowSums(select(., one_of(myCol)) == .x)))

  yes no
1   2  1
2   2  1
3   2  1

如果您实际上需要它作为每列的总和:

map_dfc(.x = c("yes", "no"),
        ~ df %>%
         summarise(!!.x := sum(rowSums(select(., one_of(myCol)) == .x))))

  yes no
1   6  3

要匹配 OPs 输出,请使用dplyrand tidyr

df %>%
 select(one_of(myCol)) %>%
 pivot_longer(everything()) %>%
 count(name, value) %>%
 pivot_wider(names_from = "name", values_from = "n", values_fill = list(n = 0))

  value  Col1  Col2  Col4
  <fct> <int> <int> <int>
1 no        1     2     0
2 yes       2     1     3
于 2020-05-28T10:53:08.867 回答
0

如果您只想计算这些列中是和否的总数,一个简单的基本 R 解决方案是:

table(sapply(myCol, function(x) df[[x]]))

#> no yes 
#>  3   6 

或者对于单个列:

sapply(myCol, function(x) table(factor(df[[x]], levels = c("no", "yes"))))
#>     Col1 Col2 Col4
#> no     1    2    0
#> yes    2    1    3
于 2020-05-28T10:59:30.763 回答