如果开始月份不是一月

时间:2016-06-01 07:16:32

标签: r dplyr

我有df data.frame,包含8年的每日价值观。

date <- rep(as.Date(seq(as.Date("2001-05-01"),
                    as.Date("2008-04-30"), by= 1), format="%Y-%m-%d"), 3)

site <- c(rep("Site_1", 2557), rep("Site_2", 2557), rep("Site_3", 2557))

value <- c(as.numeric(sample(90:271, 2557, replace=T)),
           as.numeric(sample(125:340, 2557, replace=T)),
           as.numeric(sample(70:173, 2557, replace=T)))

df <- data.frame(date, site, value)

在这种情况下,每年从五月开始,到四月结束。

我希望{3} mean的每个sd获得valueyear sites

我做了以下

df1 <- df %>%
  dplyr::mutate(year = ifelse(date < "2002-05-01", "2001-2002",
                              ifelse(date < "2003-05-01", "2002-2003",
                                     ifelse(date < "2004-05-01", "2003-2004",
                                            ifelse(date < "2005-05-01", "2004-2005",
                                                   ifelse(date < "2006-05-01", "2005-2006",
                                                          ifelse(date < "2007-05-01", "2006-2007",
                                                                 ifelse(date < "2008-05-01", "2007-2008", NA )))))))) %>%
  dplyr::select(site, year, value) %>%
  dplyr::group_by(site, year) %>%
  dplyr::summarise_each(funs(
    mean(.),
    sd(.)
  ))

它给了我想要的东西。但是,如果我有30到50年的数据,现在是时候了。另外,如果每个新的data.frame具有不同的开始月份,我每次都需要修改ifelse()以分配年份ID,以便能够按year进行分组并执行不同的计算。

如果开始月份是除1月以外的任何月份,是否有任何直接的方式来分配yearID?

2 个答案:

答案 0 :(得分:6)

怎么样?
library(dplyr)
df %>% 
  group_by(year=cut(date, seq(as.Date("2001-05-01"), as.Date("2008-05-01"), "1 year"), include.lowest = TRUE), site) %>%
  summarise(sd = sd(value), mean = mean(value)) 
# Source: local data frame [21 x 4]
# Groups: year [?]
# 
#          year   site       sd     mean
#        (fctr) (fctr)    (dbl)    (dbl)
# 1  2001-05-01 Site_1 51.82622 182.5890
# 2  2001-05-01 Site_2 63.33385 241.1260
# 3  2001-05-01 Site_3 30.04042 120.1233
# 4  2002-05-01 Site_1 51.66325 182.6658
# 5  2002-05-01 Site_2 62.87470 236.4192
# 6  2002-05-01 Site_3 28.54769 122.2329
# 7  2003-05-01 Site_1 50.97588 179.0874
# 8  2003-05-01 Site_2 63.48810 227.1230
# 9  2003-05-01 Site_3 30.87933 120.4918
# 10 2004-05-01 Site_1 53.19898 176.5589
# ..        ...    ...      ...      ...

答案 1 :(得分:6)

使用包lubridate,您可以先添加year列,如下所示:

library(lubridate) 
df$year <- ifelse(month(ymd(df$date)) < 5, 
                  paste(year(ymd(df$date))-1, year(ymd(df$date)), sep="-"),
                  paste(year(ymd(df$date)), year(ymd(df$date))+1, sep="-"))



df %>% dplyr::select(site, year, value) %>%
    dplyr::group_by(site, year) %>%
    dplyr::summarise_each(funs(
      mean(.),
      sd(.)
    ))

Source: local data frame [6 x 4]
Groups: site [1]

    site      year     mean       sd
  (fctr)     (chr)    (dbl)    (dbl)
1 Site_1 2001-2002 178.2055 54.58277
2 Site_1 2002-2003 176.9342 49.64435
3 Site_1 2003-2004 177.4153 52.20447
4 Site_1 2004-2005 179.5370 52.77848
5 Site_1 2005-2006 180.3671 51.41292
6 Site_1 2006-2007 179.3616 53.02291