简体   繁体   English

在r中的数据帧中获取时间(n + 1)-时间(n)之间的差异

[英]Getting a difference between time(n+1)-time(n) in a dataframe in r

I have a dataframe where the columns represent monthly data and the rows different simulations.我有一个数据框,其中的列代表每月数据,而行代表不同的模拟。 the data I am working with accumulates over time so I want to take the difference between the months to get the true value for that month.我正在处理的数据会随着时间的推移而累积,因此我想利用月份之间的差异来获得该月份的真实值。 There are not headers for my data frame我的数据框没有标题

For example:例如:

View(df)=

1  3  4  6  19  23  24  25  26 ...
1  2  3  4  5   6   7   8   9  ...
0  0  2  3  5   7   14  14  14 ...

My plan was to use the diff() function or something like it, but I am having trouble using it on a dataframe.我的计划是使用diff()函数或类似的东西,但我在数据帧上使用它时遇到了问题。

I have tried:我试过了:

df1<-diff(df, lag = 1, differences = 1)

but only get zeros.但只能得到零。

I am grateful for any advice.我很感激任何建议。

see ?apply .?apply If it's a data frame如果是数据框

apply(df,2,diff)

should work.应该管用。 Also since a dataframe is a list of vectors sapply(df,diff) should work.此外,由于数据帧是向量列表sapply(df,diff)应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM