通过使用 R 中的 lag() 添加到上面的行来填充列
我想用不断添加到上面行的值填充现有列。 这在 Excel 中很容易,但我还没有找到在 R 中自动化它的好方法。
如果我们在 Excel 中有 2 列,A 和 B,我们希望单元格 B2 =B1+A2,单元格 B3 = B2 +A3。我怎样才能在 R 中做到这一点?
#example dataframe
df <- data.frame(A = 0:9, B = c(50,0,0,0,0,0,0,0,0,0))
#desired output
desired <- data.frame(A = 0:9, B = c("NA",51,53,56,60,65,71,78,86,95))
我尝试使用 lag() 函数,但它没有给出正确的输出。
df <- df %>%
mutate(B = B + lag(A))
所以我做了一个有效的 for 循环,但我觉得有更好的解决方案。
for(i in 2:nrow(df)){
df$B[i] <- df$B[i-1] + df$A[i]
}
最终,我想在整个数据帧的每 n 行上迭代此函数,本质上是让求和重置每 n 行。 (任何有关如何做到这一点的提示将不胜感激!)
I want to populate an existing column with values that continually add onto the row above.
This is easy in Excel, but I haven't figured out a good way to automate it in R.
If we had 2 columns in Excel, A and B, we want cell B2 to =B1+A2, and cell B3 would = B2+A3. How can I do this in R?
#example dataframe
df <- data.frame(A = 0:9, B = c(50,0,0,0,0,0,0,0,0,0))
#desired output
desired <- data.frame(A = 0:9, B = c("NA",51,53,56,60,65,71,78,86,95))
I tried using the lag() function, but it didn't give the correct output.
df <- df %>%
mutate(B = B + lag(A))
So I made a for loop that works, but I feel like there's a better solution.
for(i in 2:nrow(df)){
df$B[i] <- df$B[i-1] + df$A[i]
}
Eventually, I want to iterate this function over every n rows of the whole dataframe, essentially so the summation resets every n rows. (any tips on how to do that would be greatly appreciated!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能接近您的需要,并使用
tidyverse
。具体来说,它使用来自purrr
的accumulate
。假设您想将每
n
行重置为零,您也可以提前使用group_by
。目前尚不完全清楚您希望如何处理第一行;在这里,它只会使用第一个
B
值并忽略第一个A
值,该值看起来与您在帖子中的内容类似。输出
This might be close to what you need, and uses
tidyverse
. Specifically, it usesaccumulate
frompurrr
.Say you want to reset to zero every
n
rows, you can also usegroup_by
ahead of time.It was not entirely clear how you'd like to handle the first row; here, it will just use the first
B
value and ignore the firstA
value, which looked similar to what you had in the post.Output
cumsum()
可用于获取您需要的结果。cumsum()
can be used to get the result you need.