【问题标题】:Within column subtraction iteratively in R在R中迭代地进行列减法
【发布时间】:2023-03-07 04:21:01
【问题描述】:

我有以下数据:

library tidyverse

age_grp <- c(10,9,8,7,6,5,4,3,2,1)
start <- c(0.420,0.420,0.420,0.420,0.420,0.420,0.420,0.420,0.420,0.420)
change <- c(0.020,0.033,0.029,0.031,0.027,0.032,0.032,0.030,0.027,0.034)
final_outcome <- c(0.400,0.367,0.338,0.307,0.28,0.248,0.216,0.186,0.159,0.125)
my_data <- data.frame(age_grp,start,change,final_outcome)

my_data1 <- my_data %>% 
  dplyr::arrange(age_grp)

我想从变量start 中的值中减去变量change 中的值,这样它就是从最老的年龄组到最年轻的年龄组的迭代递减。我希望得到的最终值在变量final_outcome 中。比如从age_grp10开始,我想从0.420减去0.20得到0.400。然后,我想从 0.400 中减去 0.033 得到 0.367 等等。我正在努力解决如何存储这些差异。我在下面进行了尝试,但我不知道如何存储差异然后继续向前(或向后,取决于你如何看待它)减法。任何意见或建议将不胜感激。

my_data1$attempt <- NA

#calculating the decreases
for(i in 2:nrow(my_data1)) 
   my_data1$attempt[i] <- round(my_data1$start[i] - my_data1$change[i-1], 4)

【问题讨论】:

    标签: r iteration subtraction


    【解决方案1】:

    如果我们需要与final_outcome中相同的输出

    library(dplyr)
    my_data %>% 
       mutate(attempt = start - cumsum(change)) %>%
       arrange(age_grp)
    

    -输出

    #  age_grp start change final_outcome attempt
    #1        1  0.42  0.034         0.125   0.125
    #2        2  0.42  0.027         0.159   0.159
    #3        3  0.42  0.030         0.186   0.186
    #4        4  0.42  0.032         0.216   0.216
    #5        5  0.42  0.032         0.248   0.248
    #6        6  0.42  0.027         0.280   0.280
    #7        7  0.42  0.031         0.307   0.307
    #8        8  0.42  0.029         0.338   0.338
    #9        9  0.42  0.033         0.367   0.367
    #10      10  0.42  0.020         0.400   0.400
    

    【讨论】:

      【解决方案2】:
      my_data$final <- my_data$start - cumsum(my_data$change) 
      
      

      【讨论】:

      • 哦!我没注意到你的
      【解决方案3】:
      library(tidyverse)
      my_data %>% 
        mutate(attempt = accumulate(change, ~ .x - .y, .init = start[1])[-1])
      

      注意:accumulate 来自 purrr 库,它是 tidyverse 的一部分。它还有一个.dir 参数,您可以在其中使用"forward""backward"

      或在基础R 中使用Reduce

      within(my_data, attempt <- Reduce("-", change, init = start[1], accumulate = T)[-1])
      

      Reduce 有一个参数right 也可以进行正向或反向计算。

      输出

         age_grp start change final_outcome attempt
      1       10  0.42  0.020         0.400   0.400
      2        9  0.42  0.033         0.367   0.367
      3        8  0.42  0.029         0.338   0.338
      4        7  0.42  0.031         0.307   0.307
      5        6  0.42  0.027         0.280   0.280
      6        5  0.42  0.032         0.248   0.248
      7        4  0.42  0.032         0.216   0.216
      8        3  0.42  0.030         0.186   0.186
      9        2  0.42  0.027         0.159   0.159
      10       1  0.42  0.034         0.125   0.125
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-12
        • 2019-08-05
        • 2015-05-14
        • 2014-02-12
        • 2021-02-07
        • 2019-09-06
        • 1970-01-01
        • 2020-09-02
        相关资源
        最近更新 更多