【问题标题】:Adding together factor levels across multiple columns将多个列中的因子水平相加
【发布时间】:2021-04-15 22:49:16
【问题描述】:

我有一个数据集,其中在多个列上重复了相同的四个因素。我正在尝试计算每列中的因子数(实际上是将行加在一起),但使用summarise( n = n()) 命令没有任何成功。而不是得到一个不。列 x 4 大小的数据框,我只计算了整个数据。

这是我尝试过的代码:

percentages_20_notconstant <- allchangingreaders_20    %>% 
 
  group_by(resp) %>%
  summarise(resp = n(colnames(allchangingreaders_20))) 
structure(list(resp = structure(c(3L, 2L, 4L, 1L, 3L, 2L, 4L, 
1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L), .Label = c("Don't Know", 
"Leave", "Remain", "Will Not Vote"), class = "factor"), euRefVoteW1 = c(0L, 
0L, 0L, 0L, 3L, 5L, 1L, 0L, 12L, 0L, 0L, 1L, 17L, 10L, 0L, 5L, 
13L, 9L, 0L, 3L), euRefVoteW2 = c(0L, 0L, 0L, 0L, 4L, 5L, 0L, 
0L, 13L, 0L, 0L, 0L, 16L, 12L, 0L, 4L, 10L, 10L, 0L, 5L), euRefVoteW3 = c(0L, 
0L, 0L, 0L, 3L, 4L, 0L, 2L, 11L, 1L, 0L, 1L, 17L, 8L, 1L, 6L, 
13L, 8L, 0L, 4L), euRefVoteW4 = c(0L, 0L, 0L, 0L, 3L, 4L, 0L, 
2L, 12L, 0L, 0L, 1L, 19L, 10L, 0L, 3L, 12L, 8L, 0L, 5L), euRefVoteW6 = c(0L, 
0L, 0L, 0L, 4L, 4L, 0L, 1L, 13L, 0L, 0L, 0L, 20L, 8L, 0L, 4L, 
13L, 7L, 0L, 5L), euRefVoteW7 = c(0L, 0L, 0L, 0L, 2L, 6L, 0L, 
1L, 13L, 0L, 0L, 0L, 18L, 14L, 0L, 0L, 11L, 12L, 0L, 2L), euRefVoteW8 = c(0L, 
0L, 0L, 0L, 2L, 7L, 0L, 0L, 12L, 1L, 0L, 0L, 19L, 12L, 0L, 1L, 
12L, 12L, 0L, 1L), euRefVoteW9 = c(0L, 0L, 0L, 0L, 4L, 5L, 0L, 
0L, 12L, 1L, 0L, 0L, 21L, 11L, 0L, 0L, 11L, 14L, 0L, 0L)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

我已经设法通过更改一个单独的函数来完成我想要做的事情,但我认为这个任务是有意义的,可以跟上。 所以我想做的事情是从第一个 dput 到这个 dput:

structure(list(resp = structure(c(3L, 2L, 4L, 1L), .Label = c("Don't Know", 
"Leave", "Remain", "Will Not Vote"), class = "factor"), euRefVoteW1 = c(45L, 
24L, 1L, 9L), euRefVoteW2 = c(43L, 27L, 0L, 9L), euRefVoteW3 = c(44L, 
21L, 1L, 13L), euRefVoteW4 = c(46L, 22L, 0L, 11L), euRefVoteW6 = c(50L, 
19L, 0L, 10L), euRefVoteW7 = c(44L, 32L, 0L, 3L), euRefVoteW8 = c(45L, 
32L, 0L, 2L), euRefVoteW9 = c(48L, 31L, 0L, 0L), Paper = structure(c(1L, 
1L, 1L, 1L), .Label = "Former Readers", class = "factor")), row.names = c(NA, 
-4L), class = c("tbl_df", "tbl", "data.frame"))

这可以用summarise 完成吗?

【问题讨论】:

  • 抱歉,现在应该可以使用了!

标签: r tidyverse


【解决方案1】:

按'resp'分组后,得到cur_data()(不包括分组列)的rowSums,然后用sum换行

library(dplyr)
allchangingreaders_20  %>% 
     group_by(resp) %>% 
     summarise(n = sum(rowSums(cur_data())), .groups = 'drop')

-输出

# A tibble: 4 x 2
#  resp              n
#* <fct>         <dbl>
#1 Don't Know       57
#2 Leave           208
#3 Remain          365
#4 Will Not Vote     2

或者如果是大于0的元素个数

allchangingreaders_20  %>% 
    group_by(resp) %>%
    summarise(n = sum(rowSums(cur_data() > 0)))
# A tibble: 4 x 2
#  resp              n
#* <fct>         <dbl>
#1 Don't Know       20
#2 Leave            27
#3 Remain           32
#4 Will Not Vote     2

更新

根据更新后的预期输出,我们也可以这样做

allchangingreaders_20  %>% 
     group_by(resp) %>% 
     summarise(across(where(is.numeric), sum), .groups = 'drop')

【讨论】:

  • 感谢@akrun,这非常有用。通过在我的工作流中进一步更改另一个函数,我已经成功地完成了我所追求的事情,并将我所追求的输出作为第二个 dput() 包含在内。特别是,我试图保留单独的列:有没有办法做到这一点?
  • @jolene 根据预期,您只需对按 'resp' 分组的各个列求和。我认为另一个答案正确地显示了它。如果早点更新就更好了
  • 抱歉,虽然我想我已经更新了另一个答案。如果我没有说清楚,对不起。感谢您的宝贵时间!
【解决方案2】:

你在找这个吗

allchangingreaders_20 %>% group_by(resp) %>%
  summarise(across(everything(), ~sum(.)))

# A tibble: 4 x 9
  resp   euRefVoteW1 euRefVoteW2 euRefVoteW3 euRefVoteW4 euRefVoteW6 euRefVoteW7 euRefVoteW8 euRefVoteW9
  <fct>        <int>       <int>       <int>       <int>       <int>       <int>       <int>       <int>
1 Don't~           9           9          13          11          10           3           2           0
2 Leave           24          27          21          22          19          32          32          31
3 Remain          45          43          44          46          50          44          45          48
4 Will ~           1           0           1           0           0           0           0           0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 2015-10-04
    • 2011-06-18
    • 2020-11-13
    相关资源
    最近更新 更多