Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有一个df包含一列的数据框,即time. 现在,我想在数据框中添加一个新列,accumulated如下所示,它总结了df.time每一行的值。
df
time
accumulated
df.time
time accumulated 1 10 10 2 12 22 3 14 36 4 6 42
一个快速的提示会非常好!
对于这些简单的任务,通常有一个内置函数。当然,了解数学运算的名称有助于找到这些函数。你想要累积总和。
df <- data.frame(time=c(10,12,14,6)) df$accumulated <- cumsum(df$time) # time accumulated #1 10 10 #2 12 22 #3 14 36 #4 6 42