如何为R中的多个列计算组内的百分比变化? [英] How can I calculate the percentage change within a group for multiple columns in R?

查看:503
本文介绍了如何为R中的多个列计算组内的百分比变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有ID列,日期列(每个ID为12个月)的数据框,并且有23个数字变量.我想获取每个ID中每个月的百分比变化.我正在使用quantmod软件包以获得百分比变化.

I have a data frame with an ID column, a date column (12 months for each ID), and I have 23 numeric variables. I would like to obtain the percentage change by month within each ID. I am using the quantmod package in order to obtain the percent change.

这里是一个只有三列的示例(为简单起见):

Here is an example with only three columns (for simplicity):

ID Date V1 V2 V3
1  Jan   2  3  5
1  Feb   3  4  6
1  Mar   7  8  9
2  Jan   1  1  1
2  Feb   2  3  4
2  Mar   7  8   8

我尝试使用dplyr和summarise_each函数,但是没有成功.更具体地说,我尝试了以下方法(train是数据集的名称):

I tried to use dplyr and the summarise_each function, but that was unsuccessful. More specifically, I tried the following (train is the name of the data set):

library(dplyr)
library(quantmod)

group1<-group_by(train,EXAMID)

foo<-function(x){
  return(Delt(x))
}

summarise_each(group1,funs(foo))

我也尝试在dplyr中使用do函数,但是我也没有成功(我猜晚上不好!).

I also tried to use the do function in dplyr, but I was not successful with that either (having a bad night I guess!).

我认为问题在于Delt函数.当我用sum函数替换Delt时:

I think that the issue is the Delt function. When I replace Delt with the sum function:

foo<-function(x){
      return(sum(x))
    }
summarise_each(group1,funs(foo))

结果是,每个变量在每个ID的日期中相加.那么每个ID的百分比如何逐月变化?

The result is that every variable is summed across the date for each ID. So how can about the percentage change month-over-month for each ID?

推荐答案

如何使用 pct <- function(x) x/lag(x)? (或(x/lag(x)-1)*100,或者您希望准确指定pct更改) 例如

How about using pct <- function(x) x/lag(x)? (or (x/lag(x)-1)*100, or however you wish to specify pct change exactly) e.g.,

pct(1:3)
[1]  NA 2.0 1.5

编辑:添加弗兰克的建议

pct <- function(x) {x/lag(x)}

dt %>% group_by(ID) %>% mutate_each(funs(pct), c(V1, V2, V3))

ID Date       V1       V2  V3
1  Jan       NA       NA  NA
1  Feb 1.500000 1.333333 1.2
1  Mar 2.333333 2.000000 1.5
2  Jan       NA       NA  NA
2  Feb 2.000000 3.000000 4.0
2  Mar 3.500000 2.666667 2.0

这篇关于如何为R中的多个列计算组内的百分比变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆