문제

I work a lot on time series data, and I want to remove a number of days (specifically it's the daylight-savings days). So I have a list of days:

days <- c("2009-03-20", "2009-10-25", "2010-03-28", "2010-10-31", 
"2011-03-27", "2011-10-30", "2012-03-25", "2012-10-28", "2013-03-31")

I want to remove all the observations on these days. Now, here is my data:

dato <-seq(as.POSIXlt("2009-01-01"), as.POSIXlt("2013-10-08"), "hour")
x<-runif(41784)*100
y<-runif(41784)*100
df<-cbind(dato, x, y)

I can pick out the days with xts, but I cant figure out how to remove these days?

도움이 되었습니까?

해결책

It seems to me your problem is that you have dates in the format yyyy-mm-dd but your dato column is in hours. If you reformat that dato column or add a column with formatted dates, you should be able to use %in% negated to remove those days. If you run the code below and check mydf2 you will see that it no longer contains any rows for the dates in drop.days. You don't need xts for this specifically (though it contains nice subsetting functions).

mydf <- data.frame(hours = seq(as.POSIXlt("2013-10-01"),
                       as.POSIXlt("2013-10-08"), "hour"),
                   val1 = runif(169)*100,
                   val2 = runif(169)*100)

mydf$date <- format(mydf$hours, format = "%Y-%m-%d")

unique(mydf$date)

drop.days <- c('2013-10-03','2013-10-04')

mydf2 <- mydf[!(mydf$date %in% drop.days), ]

다른 팁

days <- c("2009-03-20", "2009-10-25", "2010-03-28", "2010-10-31", 
          "2011-03-27", "2011-10-30", "2012-03-25", "2012-10-28", "2013-03-31")
days <- as.Date(days) # convert to Date
dato <-seq(as.POSIXlt("2009-01-01"), as.POSIXlt("2013-10-08"), "hour")
x <- runif(41784)*100
y <- runif(41784)*100
df <- data.frame(dato, x, y) # convert to data.frame not matrix
df_1 <- subset(df, !as.Date(dato) %in% days) # remove days
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top