列中行和月的天数融入时间序列R

时间:2016-04-08 16:49:05

标签: r date matrix time-series melt

我在以下结构中有时间序列数据:

dat=data.frame("Year"=rep(2005,31),
               "Day"=seq(1:31),
               "JANUARY"=sample(seq(1:100),31,T),
               "FEBRUARY"=c(sample(seq(1:100),28),NA,NA,NA),
               "MARCH"=sample(seq(1:100),31),
               "APRIL"=c(sample(seq(1:100),30),NA),
               "MAY"=sample(seq(1:100),31),
               "JUNE"=c(sample(seq(1:100),30),NA),
               "JULY"=sample(seq(1:100),31),
               "AUGUST"=sample(seq(1:100),31),
               "SEPTEMBER"=c(sample(seq(1:100),30),NA),
               "OCTOBER"=sample(seq(1:100),31),
               "NOVEMBER"=c(sample(seq(1:100),30),NA),
               "DECEMBER"=sample(seq(1:100),31)

最近我能想到的是按日和年份融化数据

melt(dat,id.vars=c("Day","Year"))

强迫约会

dat$Date<-paste(dat$Day,dat$variable,dat$Year,sep="-")
dat$Date<-as.Date(dat$Date,"%d-%B-%Y")
dat<-dat[which(is.na(pm25$Date)!=T),]

有没有更有效和非愚蠢的方式来做这些?

1 个答案:

答案 0 :(得分:0)

我使用了来自tidyr的 gather ,来自dplyr的 mutate ,来自stringr的 str_c 以及 as_date 来自lubridate。它让事情顺利进行。

library('dplyr')
library('tidyr')
library('stringr')
library('lubridate')

Dates <- dat %>% 
  gather(Month, Value, JANUARY:DECEMBER) %>% 
  mutate(Date_1 = str_c(Day, Month, Year, sep = "-"),
         Date_2 = as_date(Date_1, "%d-%B-%Y")) %>% 
  filter(!is.na(Date_2))