Setting time intervals for a plot [R] -
to previous question, dataset, how can use 20 minutes time interval.
i tried both of solution both of them showing same results. data set isn't taking values when trying convert different time interval (say 20 minute).
is possible convert data.frame() instead rather data.table(). 1 of answer given akrun:
x y date time 1 2 1-1-01 15:00 2 5 1-1-01 17:00 3 1 1-1-01 18:00 5 7 1-1-01 21:00 2 6 1-1-01 22:00 6 3 1-1-01 23:00 9 2 2-1-01 01:00 6 1 2-1-01 04:00 ..... library(data.table) dt <- setdt(df1)[, {tmp <- as.numeric(substr(time,1,2)) list(time=sprintf('%02d:00', min(tmp):max(tmp)))}, date] df1[dt, on=c('date', 'time')] dt <- setdt(df1)[, list(time=sprintf('%02d:00', 0:23)) , date] res <- df1[dt, on=c('date', 'time') ][,{tmp <- which(!(is.na(x) & is.na(y))) .sd[tmp[1l]:tmp[length(tmp)]]}] res library(zoo) res[, c('x', 'y') :=lapply(.sd, na.approx), .sdcols= x:y]
requested run following code...
df1 <- structure(list(x = c(1l, 2l, 3l, 5l, 2l, 6l, 9l, 6l), y = c(2l, 5l, 1l, 7l, 6l, 3l, 2l, 1l), date = c("1-1-01", "1-1-01", "1-1-01", "1-1-01", "1-1-01", "1-1-01", "2-1-01", "2-1-01"), time = c("15:00", "17:00", "18:00", "21:00", "22:00", "23:00", "01:00", "04:00" )), .names = c("x", "y", "date", "time"), class = "data.frame", row.names = c(na, -8l)) library(chron) library(data.table) time<-as.character(substr(times(00:71/72),1,5)) dates <- paste0(1:2,'-1-01') all.dt <- expand.grid(date=dates,time=time) big.data <- merge(all.dt, df1, all.x=true)
now last part can fill na running following code
library(zoo) big.data <- within(big.data,{ x <- na.approx(x,na.rm=false) y <- na.approx(y,na.rm=false) })
Comments
Post a Comment