R - 立即组合并求和

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了R - 立即组合并求和相关的知识,希望对你有一定的参考价值。

这是我的意见。

df <- data.frame(n1 = c("A1", "A1", "A1", "A1", "A1", "A1", "A1"),
                 n2 = c("B", "B", "B", "B", "C", "C", "C"),
                 n3 = c("D", "D", "E", "E", "F", "G", "H"),
                 n4 = c("I", "J", "K", "L", "M", "N", "O"),
                 n5 = c(1:7),
                 n6 = c(1:7),
                 n7 = c(1:7))

library(dplyr)

df <- split(df, df$n3)

   df <- lapply(df, function(x) {
   out <- x %>% 
    group_by() %>% 
    summarise(n1 = "", 
              n2 = "",
              n3 = paste0(unique(n3), " Total"),
              n4 = "",
              n5 = sum(n5, na.rm = T),
              n6 = sum(n6, na.rm = T),
              n7 =  sum(n7, na.rm = T)) %>% 
    ungroup()
  bind_rows(x, out)
})

df <- do.call("rbind", df)

我希望我的输出像这张图片一样。但是使用我的代码,它将在每列中汇总和总计。

如何使用列n1,n2,n3的组一次对行进行求和?

答案

使用dplyr的解决方案。

# Create example data frame
df <- data.frame(n1 = c("A1", "A1", "A1", "A1", "A1", "A1", "A1"),
                 n2 = c("B", "B", "B", "B", "C", "C", "C"),
                 n3 = c("D", "D", "E", "E", "F", "G", "H"),
                 n4 = c("I", "J", "K", "L", "M", "N", "O"),
                 n5 = c(1:7),
                 n6 = c(1:7),
                 n7 = c(1:7),
                 stringsAsFactors = FALSE)

library(dplyr)

df2 <- df %>%
  group_by(n1, n2, n3) %>%
  summarise_at(vars(n5, n6, n7), funs(sum(.))) %>%
  mutate(n3 = paste(n3, "Total", sep = " ")) %>%
  bind_rows(df) %>%
  arrange(n1, n2, n3)

df3 <- df %>%
  group_by(n1, n2) %>%
  summarise_at(vars(n5, n6, n7), funs(sum(.))) %>%
  mutate(n2 = paste(n2, "Total")) %>%
  bind_rows(df2) %>%
  arrange(n1, n2) 

df4 <- df %>%
  group_by(n1) %>%
  summarise_at(vars(n5, n6, n7), funs(sum(.))) %>%
  mutate(n1 = paste(n1, "Total")) %>%
  bind_rows(df3) %>%
  arrange(n1) %>%
  mutate_all(funs(replace(., is.na(.), ""))) %>%
  mutate_at(vars(n1, n2), funs(ifelse(grepl("Total", n3), "", .))) %>%
  mutate(n1 = ifelse(grepl("Total", n2), "", n1)) %>%
  select(colnames(df))
df4
# # A tibble: 15 x 7
#          n1      n2      n3    n4    n5    n6    n7
#       <chr>   <chr>   <chr> <chr> <chr> <chr> <chr>
#  1       A1       B       D     I     1     1     1
#  2       A1       B       D     J     2     2     2
#  3                  D Total           3     3     3
#  4       A1       B       E     K     3     3     3
#  5       A1       B       E     L     4     4     4
#  6                  E Total           7     7     7
#  7          B Total                  10    10    10
#  8       A1       C       F     M     5     5     5
#  9                  F Total           5     5     5
# 10       A1       C       G     N     6     6     6
# 11                  G Total           6     6     6
# 12       A1       C       H     O     7     7     7
# 13                  H Total           7     7     7
# 14          C Total                  18    18    18
# 15 A1 Total                          28    28    28

以上是关于R - 立即组合并求和的主要内容,如果未能解决你的问题,请参考以下文章

使用分支/合并框架执行并行求和

R语言split函数unsplit函数按组拆分数据合并数据实战

在一行上组合并打印不同的变量

二路归并排序(也叫合并排序)

不立即分离片段

如何根据组对一列数据求和