R,新列,即行的总和
R, New Column that's a Sum to Row
我正在尝试找到一种简单的方法来创建一个新变量,该变量将给我一列的总和,直到某一行。我输入了一个新变量“Spend.to.Date”来说明我在寻找什么。我可以通过指定要加在一起的特定单元格来“手动”完成,但我想必须有一种更有效的方法来实现它。任何帮助将不胜感激!
df <- as.data.frame(matrix(c(1, 2, 3, 4, 5, 100, 200, 300, 400, 500), 5, 2)) #Take as given
colnames(df) <- c("Day", "Spend")
df$Spend.to.Date <- c(100, 300, 600, 1000, 1500)
df$Spend.to.Date <- cumsum(df$Spend)
我正在尝试找到一种简单的方法来创建一个新变量,该变量将给我一列的总和,直到某一行。我输入了一个新变量“Spend.to.Date”来说明我在寻找什么。我可以通过指定要加在一起的特定单元格来“手动”完成,但我想必须有一种更有效的方法来实现它。任何帮助将不胜感激!
df <- as.data.frame(matrix(c(1, 2, 3, 4, 5, 100, 200, 300, 400, 500), 5, 2)) #Take as given
colnames(df) <- c("Day", "Spend")
df$Spend.to.Date <- c(100, 300, 600, 1000, 1500)
df$Spend.to.Date <- cumsum(df$Spend)