【问题标题】:How to add tapply results to an existing data frame [duplicate]如何将tapply结果添加到现有数据框[重复]
【发布时间】:2014-09-01 11:18:04
【问题描述】:

我想将tapply 结果作为新列添加到原始数据框中。

这是我的数据框:

 dat <- read.table(text = " category birds    wolfs     snakes
                   yes        3        9         7
                   no         3        8         4
                   no         1        2         8
                   yes        1        2         3
                   yes        1        8         3
                   no         6        1         2
                   yes        6        7         1
                   no         6        1         5
                   yes        5        9         7
                   no         3        8         7
                   no         4        2         7
                   notsure    1        2         3
                   notsure    7        6         3
                   no         6        1         1
                   notsure    6        3         9
                   no         6        1         1   ",header = TRUE)

我想将每个类别的平均值作为一列添加到数据框中。 我使用:tapply(dat$birds, dat$category, mean) 来获取每个类别的平均值,但我没有找到将其添加到数据集中的方法,以至于在新列中我将获得相关类别的平均值。

【问题讨论】:

    标签: r dataframe tapply


    【解决方案1】:

    你可以从base使用ave

      dat$mbirds <- with(dat, ave(birds, category, FUN=mean))
    

    如果你想使用tapply

      mbirds1 <- with(dat, tapply(birds, category, mean))
      dat$mbirds1 <- mbirds1[match(dat$category,names(mbirds1))]
    
      head(dat)
      #  category birds wolfs snakes mbirds mbirds1
     #1      yes     3     9      7  3.200   3.200
     #2       no     3     8      4  4.375   4.375
     #3       no     1     2      8  4.375   4.375
     #4      yes     1     2      3  3.200   3.200
     #5      yes     1     8      3  3.200   3.200
     #6       no     6     1      2  4.375   4.375
    

    或者你可以使用data.table,这样会很快

     library(data.table)
     setDT(dat)[,mbirds1:= mean(birds), by=category]
    

    【讨论】:

      【解决方案2】:

      这是aggregate 的答案。在它的参数中使用一个公式使它变得既简单又好。

      > a <- aggregate(birds~category, dat, mean)
      > cb <- cbind(dat, mean = a[,2][match(dat[[1]], a[,1])])
      > head(cb)
      #  category birds wolfs snakes  mean
      #1      yes     3     9      7 3.200
      #2       no     3     8      4 4.375
      #3       no     1     2      8 4.375
      #4      yes     1     2      3 3.200
      #5      yes     1     8      3 3.200
      #6       no     6     1      2 4.375
      

      【讨论】:

        【解决方案3】:

        您可以使用这样的dplyr 包轻松实现这一目标

        dat &lt;- dat %&gt;% group_by(category) %&gt;% mutate(mbirds=mean(birds))

        更多关于 dplyr 包的信息可以在here找到。

        您可以在 akrun 的回答中找到其他包的方法。

        【讨论】:

        • 非常感谢@iugrina,但是当我写名字(dat)时,我没有得到新变量“mbirds”。如何将其添加到原始数据框中?我还有两个问题: %>% 的含义是什么?另外,你有什么想法如何在不需要 dplyr 包的情况下做到这一点?
        • 我会调整我的答案
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-28
        • 2016-01-25
        • 1970-01-01
        • 1970-01-01
        • 2020-04-27
        • 2019-04-22
        相关资源
        最近更新 更多