计算变化率 [英] Calculating Rate of Change

查看:33
本文介绍了计算变化率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为yield"的数据集:

I have a data set called "yield":

yield <- data.frame(fruits = c("apples", "apples", "apples", "oranges", "oranges", 
         "oranges", "pears", "pears", "pears"), year = rep(2008:2010, 3), 
         count = c(10, 13, 7, 5, 12, 14, 16, 18, 20))

我想确定哪种水果在 2008 年到 2010 年之间的变化率最大.我得到的最接近的是:

I want to determine which fruit has the largest rate of change between 2008 and 2010. The closest I have gotten to was:

diff(yield$count)/yield[-nrow(yield),] * 100

但它不仅影响我的 fruitsyear 列,结果不正确.

but not only does it affect my fruits and year columns, the results are incorrect.

推荐答案

根据您的公式,我认为这个 dplyr 解决方案有效.您需要按水果分组,然后按年份排序,lag 才能正常工作:

Based on your formula, I think this dplyr solution works. You need to group by fruit and then order by year, for lag to work correctly:

library(dplyr)
yield %>% 
  group_by(fruits) %>% 
  arrange(fruits, year) %>% 
  mutate(rate = 100 * (count - lag(count))/lag(count)) %>%
  ungroup()

# A tibble: 9 x 4
  fruits   year count   rate
  <fct>   <int> <dbl>  <dbl>
1 apples   2008 10.0    NA  
2 apples   2009 13.0    30.0
3 apples   2010  7.00 - 46.2
4 oranges  2008  5.00   NA  
5 oranges  2009 12.0   140  
6 oranges  2010 14.0    16.7
7 pears    2008 16.0    NA  
8 pears    2009 18.0    12.5
9 pears    2010 20.0    11.1

这篇关于计算变化率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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