你如何计算R中不同年份的相同月份之间的差异

时间:2014-02-10 18:51:24

标签: r date

我有这个名为df的数据框。行是几个月,rowname是年。我想订购1月至12月的月份,并希望计算不同年份相同月份之间的差异%。例如,我想知道1月,2月等2009年和2008年之间的百分比差异。这样做几个月。

这是我的df:

df <- structure(list(YEAR = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 
2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 
6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 
4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 
2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 
6L), .Label = c("2008", "2009", "2010", "2011", "2012", "2013"
), class = "factor"), M = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 
4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 
7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 
10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 
12L, 12L, 12L, 12L, 12L), .Label = c("Apr", "Aug", "Dec", "Feb", 
"Jan", "Jul", "Jun", "Mar", "May", "Nov", "Oct", "Sep"), class = "factor"), 
    Freq = c(93221016, 124800455, 224127360, 287150001, 318228530, 
    387573710, 98811936, 171940117, 239581603, 294965702, 336269471, 
    406584525, 112958413, 215853263, 282293439, 314483537, 355561387, 
    386086538, 89354868, 109900379, 206640377, 268944957, 322896485, 
    356774443, 91007916, 113469678, 220743958, 284697404, 324823553, 
    373885187, 96887316, 158230269, 242175673, 284271058, 335464023, 
    397269760, 90091044, 143862802, 232512479, 262275285, 324988644, 
    388064866, 93936288, 139665422, 213302607, 297847827, 329044914, 
    386372600, 99646750, 139195786, 229651074, 277779620, 324395065, 
    397346365, 106477407, 197698621, 256559666, 242683830, 347193478, 
    430880720, 100909236, 185392147, 258317251, 238847338, 349017727, 
    422523576, 96888876, 170467493, 240815506, 285132804, 324063033, 
    389471906)), .Names = c("YEAR", "M", "Freq"), row.names = c(NA, 
-72L), class = "data.frame")

有没有一种简单的方法可以做到这一点,可能是R?

中的包

3 个答案:

答案 0 :(得分:2)

试试这个:

library(zoo)

z <- read.zoo(df, index = 1:2, FUN = function(y, m) as.yearmon(paste(y, m), "%Y %b") )
diff(z, 12, arithmetic = FALSE)

或稍微更紧凑(仅##行已更改):

library(zoo)
library(gsubfn)
z <- fn$read.zoo(df, index = 1:2, FUN = ~ as.yearmon(paste(y, m), "%Y %b") ) ##
diff(z, 12, arithmetic = FALSE)

添加了紧凑的表格。

答案 1 :(得分:2)

此命令将添加一个新列,其中包含年份之间的差异(百分比):

transform(df, diff = ave(Freq, M, FUN = function(x) 
  c(0, (diff(x) / head(x, -1)) * 100)))

答案 2 :(得分:0)

在dplyr:

library(dplyr)
df %.% 
  arrange(M, YEAR) %.% 
  group_by(M) %.% 
  mutate(lag_Freq = lag(Freq), z = (Freq - lag_Freq)/lag_Freq)

Source: local data frame [72 x 5]
Groups: M

   YEAR   M      Freq  lag_Freq           z
1  2008 Apr  93221016        NA          NA
2  2009 Apr 124800455  93221016  0.33875879
3  2010 Apr 224127360 124800455  0.79588576
4  2011 Apr 287150001 224127360  0.28119120
5  2012 Apr 318228530 287150001  0.10823099
6  2013 Apr 387573710 318228530  0.21791000
7  2008 Aug  98811936        NA          NA
8  2009 Aug 171940117  98811936  0.74007437
9  2010 Aug 239581603 171940117  0.39340142
10 2011 Aug 294965702 239581603  0.23117008
11 2012 Aug 336269471 294965702  0.14002906
12 2013 Aug 406584525 336269471  0.20910329