簡體   English   中英

將多個列中的因子水平相加

[英]Adding together factor levels across multiple columns

我有一個數據集,在多個列上重復了相同的四個因素。 我正在嘗試計算每列中的因子數(實際上是將行加在一起),但使用summarise( n = n())命令沒有任何成功。 而不是得到一個不。 列 x 4 大小 dataframe,我只計算了整個事情。

這是我嘗試過的代碼:

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"))

我已經設法通過更改單獨的 function 來完成我想要做的事情,但認為這項任務是有意義的。 所以我想做的是從第一個輸入到這個輸入的 go :

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完成嗎?

按 'resp' 分組后,獲取rowSums 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

更新

基於更新的預期output,我們還可以做

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

你在找這個嗎

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM