借助逆向功能,Im 想建立一个新的一栏数据,该一栏总和在第一栏中计算,但使用不超过<代码>incr的加权。
That could start with df
incr <- 1.5
df <- data.frame(a = c(6,0,0,2.5,0,0))
df
a
1 6.0
2 0.0
3 0.0
4 2.5
5 0.0
6 0.0
then create a new column b
using incr
:
a b
1 6.0 1.5
2 0.0 1.5
3 0.0 1.5
4 2.5 1.5
5 0.0 1.5
6 0.0 1.0
我发现<代码>tidyr:uncount(),这似乎是一种可以发挥作用的职能,但我需要使用非强迫加薪。 通常试图改变和使用病媒功能,并想将其操作起来<>代码>rowwise(),但我的大多数想法都需要加以利用。
From comment below: If analogies are helpful, think of it like queued downloads. Column a
shows you press to download 6 MB at time 1, 0 MB for times 2 and 3, then you press to download 2.5 MB at time 4. Your connection, however, can only download at the speed of incr
. So, if incr
is 1.5, column b
shows what actually downloaded. You fully use that connection speed each period until you download the final residual (1.0
) in time 6.
为了更好地强调层面,这里还有一点:
incr <- 1.5
df <- data.frame(a = rep(0,100),b=rep(0,100))
df$a[c(30,33,38)] = c(6,2.5,1)
df[30:39,]
a b
30 6.0 0
31 0.0 0
32 0.0 0
33 2.5 0
34 0.0 0
35 0.0 0
36 0.0 0
37 0.0 0
38 1.0 0
39 0.0 0
具有预期产出
a b
30 6.0 1.5
31 0.0 1.5
32 0.0 1.5
33 2.5 1.5
34 0.0 1.5
35 0.0 1.0
36 0.0 0.0
37 0.0 0.0
38 1.0 1.0
39 0.0 0.0