[英]Calculating Rate of Change
我有一個名為“產量”的數據集:
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 年之間變化率最大。我得到的最接近的是:
diff(yield$count)/yield[-nrow(yield),] * 100
但它不僅影響我的fruits
和year
列,結果也不正確。
根據您的公式,我認為此dplyr
解決方案有效。 您需要按水果分組,然后按年份訂購,以便lag
正常工作:
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
為了完整data.table
,這里與data.table
相同。
R> library(data.table)
R> df <- data.frame(fruits=rep(c("apples", "oranges", "pears"), each=3),
+ year=rep(2008:2010, 3),
+ count=c(10,13,7,5,12,14,16,18,20))
R> dt <- as.data.table(df)
R> dt
fruits year count
1: apples 2008 10
2: apples 2009 13
3: apples 2010 7
4: oranges 2008 5
5: oranges 2009 12
6: oranges 2010 14
7: pears 2008 16
8: pears 2009 18
9: pears 2010 20
R>
R> dt[ , .(year, change=100*(count-shift(count,1))/shift(count,1)), by=fruits]
fruits year change
1: apples 2008 NA
2: apples 2009 30.0000
3: apples 2010 -46.1538
4: oranges 2008 NA
5: oranges 2009 140.0000
6: oranges 2010 16.6667
7: pears 2008 NA
8: pears 2009 12.5000
9: pears 2010 11.1111
R>
我們將by=fruits
分組by=fruits
並在每個塊中顯示year
和所需的變化率為100*(current-prev)/prev
,其中我們使用shift()
來滯后count
系列。
基地 R 一個班輪:
yield$roc <- with(yield, ave(count, fruits, FUN = function(x){c(0, c(diff(x), 0)/x)[seq_along(x)]}))
如果您希望 NA 代替 0 和實際百分比率(即 *100),則基於 R:
yield$roc <- with(yield, ave(count, fruits,
FUN = function(x){c(NA_real_, c(diff(x), 0)/x)[seq_along(x)] * 100}))
基礎 R 函數:
roc <- function(ordered_num_vec){
c(0, c(diff(ordered_num_vec), 0) / ordered_num_vec)[seq_along(ordered_num_vec)]
}
with(yield, ave(count, fruits, FUN = roc))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.