Question

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?

Was it helpful?

Solution

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), ]

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top