[英]How to do rolling percent change on non-time series in R?
I have a dataframe with the following sample:我有一个带有以下示例的 dataframe:
df = data.frame(x1 = c(2000a,2010a,2000b,2010b,2000c,2010c),
x2 = c(1,2,3,4,5,6))
I am trying to find a way to calculate the percent change for each "group" (a,b,c) using the change()
function.我试图找到一种方法来使用
change()
function 计算每个“组”(a,b,c)的百分比变化。 Below is my attempt:以下是我的尝试:
percent_change = change(df,x2, NewVar = "percent_change", slideBy = 1,type = 'percent')
where slideBy is the lag variable that restarts the percent change calculation every other observation.其中 slideBy 是滞后变量,它每隔一次观察重新开始百分比变化计算。 This does not work, and I get the following error:
这不起作用,我收到以下错误:
" Remember to put data in time order before running. " 记得在运行前把数据按时间排序。
Leading total_units by 1 time units."领先 total_units 1 个时间单位。”
Would it be possible to adapt my x1 column to a time series or is there an easier way around this I am missing?是否可以将我的 x1 列调整为时间序列,或者有没有更简单的方法来解决我错过的问题?
Thank you!谢谢!
This uses the data.table structure from the data.table package.这使用来自 data.table package 的 data.table 结构。 First it sorts on x1, then does a row by row calculation of the percent change, grouping by the letter in x1.
首先它按 x1 排序,然后逐行计算百分比变化,按 x1 中的字母分组。
library(data.table)
setDT(df)
df[order(x1),
100*x2/shift(x2,1L),
keyby=gsub("[0-9]","",x1)]
Here is a tidyverse way to do this.这是一个 tidyverse 方法来做到这一点。 First, use extract to separate x1 into year and group, then pivot_wider on the table.
首先,使用extract将 x1 分成 year 和 group,然后在 table 上使用pivot_wider 。 Now you can use mutate to create the percent change row.
现在您可以使用 mutate 创建百分比变化行。
library(dplyr)
library(tidyr)
df = data.frame(x1 = c("2000a","2010a","2000b","2010b","2000c","2010c"),x2 = c(1,2,3,4,5,6))
df_new = df %>%
extract(x1, c("year", "group"),regex="(\\d{4})(\\D{1})") %>%
pivot_wider(names_from = year, values_from=x2) %>%
mutate(percent_change=(`2010`-`2000`)/`2000`)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.