【问题标题】:Creating new variable to add up information from other observations conditionally创建新变量以有条件地从其他观察中添加信息
【发布时间】:2020-10-22 03:57:33
【问题描述】:

这可能是一个完全新手的问题,但对于我的生活,我无法弄清楚如何去做。所以假设我有一个看起来像这样的数据框。

id<- c(1,1,2,3,3,3,4,5,5)
type<- c('a','a', 'b', 'a', 'a', 'b', 'a', 'b', 'b')
percent<- c(.2,.4,.5, .1,.2, .4, .2, .6, .1)

dummy_data<- cbind(id, type, percent)

我要做的是创建一个新变量,我们称之为 total_percent,它执行以下操作:如果 id 相同且类型相同,则将它们相加。例如在虚拟数据中,由于 ID 为 1 的两个观测值都是类型 a,因此在每个观测值的新变量中返回 0.6。在 id 为 3 的观察中,两个具有类型 a,一个具有类型 b。因为在每次观察中,a 类型的两个加起来是 0.3,b 类型的加起来是 0.4。

数据应该是这样的。

total_percent<- c(.6, .6, .5, .3, .3, .4, .2, .7, .7)
final_data<- cbind(id, type, percent, total_percent)

任何帮助将不胜感激!我完全被难住了。

【问题讨论】:

    标签: r data-manipulation


    【解决方案1】:

    试试这个:

    library(dplyr)
    
    dummy_data %>% group_by(id,type) %>% mutate(Index=cumsum(percent),Result=max(Index))
    
    # A tibble: 9 x 5
    # Groups:   id, type [6]
         id type  percent Index Result
      <dbl> <fct>   <dbl> <dbl>  <dbl>
    1     1 a         0.2   0.2    0.6
    2     1 a         0.4   0.6    0.6
    3     2 b         0.5   0.5    0.5
    4     3 a         0.1   0.1    0.3
    5     3 a         0.2   0.3    0.3
    6     3 b         0.4   0.4    0.4
    7     4 a         0.2   0.2    0.2
    8     5 b         0.6   0.6    0.7
    9     5 b         0.1   0.7    0.7
    

    【讨论】:

    • 效果很好,谢谢!我没有意识到我可以像这样组合 group by 和 mutate 命令。你是救生员!
    • @tfr950 太好了!当然,您可以将它们组合在一起。请,如果可以,请接受答案!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 2017-10-30
    • 1970-01-01
    • 2021-07-30
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多