[英]In R, how do you define a new column in a df as a function of a subset of another column?
I have time series data in a data frame. 我在数据框中有时间序列数据。 Let's say I have one column of dates, one column of values, and I want to create a new column that is the sum of all values before the date index. 假设我有一列日期,一列值,并且我想创建一个新列,该列是日期索引之前所有值的总和。 Is there a better solution than using a for loop? 有没有比使用for循环更好的解决方案?
For example: 例如:
table = data.frame(date=seq(1,5),values=c(3,2,4,1,5))
table$sum = ...??
For this you can use cumsum
: 为此,您可以使用cumsum
:
table = transform(table, sum_value = cumsum(values))
table
date values sum_value
1 1 3 3
2 2 2 5
3 3 4 9
4 4 1 10
5 5 5 15
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.