【问题标题】:How do I sum up the components of a column that corresponds to the particular value of another column?如何总结与另一列的特定值相对应的列的组件?
【发布时间】:2021-06-16 18:06:24
【问题描述】:

如果我的写作看起来很麻烦,请看附图。 enter image description here 我想要通用代码,这样我就可以将它应用于一个非常大的文件。 2/3 行/列的公式的任何数字计算对我没有帮助。假设我有以下 CSV 表:

Class1  Count1
1        1
2        1
1        0
3        1
1        1
1        1
3        0
3        1
2        1

现在我想根据新列中的类值进行求和。假设我们得到两个名为 Class2 和 Count2 的新列,根据我的需要,Count2 的最顶部单元格(第一行)应该具有与“Class1”列中的“1”对应的“Count1”列中所有值的总和,Count2 的倒数第二个最上面的单元格(在第 2 行)应该具有与 Class1 中的“2”相对应的“Count1”列中所有值的总和,依此类推。运行代码后,我想要一个如下表:

Class1  Count1  Class1  Count2
1        1        1       3
2        1        2       2
1        0        3       2
3        1
1        1
1        1
3        0
3        1
2        1

我从事社会科学工作,所以这类工作对我来说非常艰巨。如果有人给我 R 代码来执行它,那将有很大帮助。提前致谢。

【问题讨论】:

  • 你可以试试cbind(df1, rbind(as.matrix(aggregate(Count1~Class1, df1, sum)), matrix(NA, nrow(df1) - length(unique(df1$Class1)), ncol(df1)))) df1 是你的csv表

标签: r


【解决方案1】:

您可以使用来自dplyr 包的summarise: 先按 Class1 分组,然后求和。

library(dplyr)
df %>% 
  group_by(Class1) %>% 
  summarize(Count2 = sum(Count1))

输出:

  Class1 Count2
*  <dbl>  <dbl>
1      1      3
2      2      2
3      3      2

数据:

df <- structure(list(Class1 = c(1, 2, 1, 3, 1, 1, 3, 3, 2), Count1 = c(1, 
1, 0, 1, 1, 1, 0, 1, 1)), row.names = c(NA, -9L), class = c("tbl_df", 
"tbl", "data.frame"))

【讨论】:

    【解决方案2】:

    这是一个基本的 R 解决方案。

    y <- with(df1, table(Count1 * Class1))
    y <- as.data.frame(y[names(y) != 0])
    
    y
    #  Var1 Freq
    #1    1    3
    #2    2    2
    #3    3    2
    

    如果需要,可以更改名称。

    names(y) <- c("Class2", "Count2")
    

    要合并输入数据和此结果,请使用merge,请参阅@G.Grothendieck, point 1

    merge(df1, y, by = 0, all = TRUE)[-1]
    #  Class1 Count1 Class2 Count2
    #1      1      1      1      3
    #2      2      1      2      2
    #3      1      0      3      2
    #4      3      1   <NA>     NA
    #5      1      1   <NA>     NA
    #6      1      1   <NA>     NA
    #7      3      0   <NA>     NA
    #8      3      1   <NA>     NA
    #9      2      1   <NA>     NA
    

    数据

    df1 <- read.table(text = "
    Class1  Count1
    1        1
    2        1
    1        0
    3        1
    1        1
    1        1
    3        0
    3        1
    2        1
    ", header = TRUE)
    

    【讨论】:

      【解决方案3】:

      base R 中的rowsumtransform 选项

      transform(df1, Class2 = `length<-`(unique(Class1), nrow(df1)),
           Count2 = `length<-`(rowsum(Count1, Class1)[,1], nrow(df1)))
      

      -输出

         Class1 Count1 Class2 Count2
      1      1      1      1      3
      2      2      1      2      2
      3      1      0      3      2
      4      3      1     NA     NA
      5      1      1     NA     NA
      6      1      1     NA     NA
      7      3      0     NA     NA
      8      3      1     NA     NA
      9      2      1     NA     NA
      

      -输出

      df1 <- structure(list(Class1 = c(1L, 2L, 1L, 3L, 1L, 1L, 3L, 3L, 2L), 
          Count1 = c(1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L)), 
          class = "data.frame", row.names = c(NA, 
      -9L))
      

      【讨论】:

        猜你喜欢
        • 2021-02-22
        • 2021-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多