Multiply previous row value by constant R

前端 未结 2 1550
南笙
南笙 2020-12-20 21:07

Have a simple R question but cannot seem to find an answer:

I have a data frame like this:

assumption_val year
1.2            2015
0              201         


        
2条回答
  •  失恋的感觉
    2020-12-20 21:46

    data <- read.table(textConnection("
        assumption_val year
        1.2            2015
        0              2016
        0              2017
        0              2018
        0              2019"), header = TRUE)
    
    data$assumption_val <- data$assumption_val[1]^(1:nrow(data))
    
    data
    ##   assumption_val year
    ## 1        1.20000 2015
    ## 2        1.44000 2016
    ## 3        1.72800 2017
    ## 4        2.07360 2018
    ## 5        2.48832 2019
    

提交回复
热议问题