简体   繁体   English

按日期减去 R 数据框中的行

[英]Subtract rows in R dataframes by dates

I have a dataframe, df , like this one:我有一个 dataframe, df ,就像这个:

           Date Colombia    Chile
2015-01-01  2015 38.12333 31.92088
2020-01-01  2020 44.95952 38.10776

And I would like to substract the 2015 values, for each country, from the values in 2020, so I could get a new dataframe looking loke this one:我想从 2020 年的值中减去每个国家/地区的 2015 年值,这样我就可以得到一个新的 dataframe 看起来像这个:

           Date Colombia    Chile
2020-01-01  2020 6.83619    6.18688

But I would like to do it by dates, ie, the identifier is that the value of the lower date is subtracted from the value of the higher date in each country.但是我想按日期来做,即标识符是从每个国家/地区的较高日期的值中减去较低日期的值。

Can you guys help me out?你们能帮帮我吗?

diff(as.matrix(df))
           Date Colombia   Chile
2020-01-01    5  6.83619 6.18688

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

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