在r续中使用上一个计算的行值 [英] Use previous calculated row value in r Continued

查看:136
本文介绍了在r续中使用上一个计算的行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个data.table如下:

  DT<  -  data.table(A = 1: 20,B = 1:20 * 10,C = 1:20 * 100)
DT
ABC
1:1 10 100
2:2 20 200
3:3 30 300
4:4 40 400
5:5 50 500
...
20:20 200 2000



我想要计算一个新列G,其第一个值为B列中前20行的平均值值,然后我想使用列G的第一行来帮助计算G的下一行值。



说出列B的前20行的平均值为105,G中下一行的公式为:DT $ G [2] = DT $ G [1] * 2,下一行为DT $ G [3] = DT $ G [2] 2。这意味着第一个值不应再次用于下一行,依此类推。

  ABCG 
1:1 10 100 105
2:2 20 200 210
3 :3 30 300 420
4:4 40 400 840
5:5 50 500 1680
...
20:20 200 2000 55050240

有任何想法吗?

解决方案

您可以用一个小算术来做到这一点:

  DT $ G < - 平均值:20])
DT $ G < - DT $ G * cumprod(rep(2,nrow(DT)))/ 2

或使用 data.table 语法,由@DavidArenburg提供:

  DT [,G:= mean(B [1:20])* cumprod(rep(2,.N))/ 2] 

或从@Frank

  DT $ G ;  -  cumprod(c(mean(head(DT $ B,20)),rep(2,nrow(DT)-1)))


I have a data.table that looks like this:

DT <- data.table(A=1:20, B=1:20*10, C=1:20*100)
DT
    A  B   C
1:  1  10  100
2:  2  20  200
3:  3  30  300
4:  4  40  400
5:  5  50  500
...
20: 20 200 2000

I want to be able to calculate a new column "G" that has the first value as the average of the first 20 rows in column B as the first value, and then I want to use the first row of column G to help calculate the next row value of G.

Say the Average of the first 20 rows of column B is 105, and the formula for the next row in G is: DT$G[2] = DT$G[1]*2, and the next row again is DT$G[3]=DT$G[2]*2. This means that the first value should not be used again in the next row and so forth.

    A    B   C       G
1:  1   10   100     105
2:  2   20   200     210
3:  3   30   300     420
4:  4   40   400     840
5:  5   50   500     1680
...
20: 20  200  2000    55050240

Any ideas on this would be made?

解决方案

You can do this with a little arithmetic:

DT$G <- mean(DT$B[1:20])
DT$G <- DT$G * cumprod(rep(2,nrow(DT)))/2

Or using data.table syntax, courtesy of @DavidArenburg:

DT[ , G := mean(B[1:20]) * cumprod(rep(2, .N)) / 2]

or from @Frank

DT$G <- cumprod(c( mean(head(DT$B,20)), rep(2,nrow(DT)-1) ))

这篇关于在r续中使用上一个计算的行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆