Matt
Matt

Reputation: 333

Expand dates and times in data frame with missing for each parameter

I have the following data set:

Date<-c("2013-01-01 02:00:00","2013-01-02 02:00:00","2013-01-03 02:00:00","2013-01-01 02:00:00","2013-01-02 02:00:00","2013-01-03 02:00:00")
Parameter<-c("Par1","Par1","Par1","Par2","Par2","Par2")
conc<-c(1,4,3,2,6,5)
obs<-data.frame(Date,Parameter,conc)
obs$Date<-as.POSIXct(obs$Date)
obs$Parameter<-as.factor(obs$Parameter)
obs$conc<-as.numeric(obs$conc)

For each day between 2013-01-01 and 2013-01-03 there was an observation made at 02:00:00 hours for each of the 2 parameters. I will need to expand this data set to full 24-hour days and also take the parameter and value for each missing time per each day. I tried it with creating a new data.frame with all dates and times between 2013-01-01 and 2013-01-03 and then merging it with the obs data frame but I cannot figure out how to use each parameter name and observation value for the missing dates and times. The result should look like this:

Date                       Parameter conc
2013-01-01 00:00:00      Par1        1
2013-01-01 01:00:00      Par1        1
2013-01-01 02:00:00      Par1        1
.
. 
.
2013-01-01 23:00:00      Par1        1
2013-01-02 00:00:00      Par1        4
2013-01-02 01:00:00      Par1        4  
2013-01-02 02:00:00      Par1        4
.
.
.
2013-01-02 23:00:00      Par1        4   
2013-01-03 00:00:00      Par1        3
2013-01-03 01:00:00      Par1        3
2013-01-03 02:00:00      Par1        3
.
.
.
2013-01-03 23:00:00      Par1        3
2013-01-01 00:00:00      Par2        2
2013-01-01 01:00:00      Par2        2
2013-01-01 02:00:00      Par2        2
.
.
.
2013-01-01 23:00:00      Par2        2
2013-01-02 00:00:00      Par2        6
2013-01-02 01:00:00      Par2        6   
2013-01-02 02:00:00      Par2        6
.
.
.
2013-01-02 23:00:00      Par2        6
2013-01-03 00:00:00      Par2        5
2013-01-03 01:00:00      Par2        5
2013-01-03 02:00:00      Par2        5
.
.
.
2013-01-03 23:00:00      Par2        5

Upvotes: 0

Views: 77

Answers (2)

HolgerBarlt
HolgerBarlt

Reputation: 317

As an alternative answer:

new.Date <- matrix(sapply(obs$Date,function(t) t + as.difftime(-2:21,units = "hours")),ncol=1)
new.Par  <- matrix(sapply(obs$Parameter,function(x)rep(x,24)),ncol=1)
new.conc <- matrix(sapply(obs$conc,function(x)rep(x,24)),ncol=1)

newData <- data.frame(
  Date      = as.POSIXct(new.Date, origin = "1970-01-01"),
  Parameter = new.Par,
  new.conc  = new.conc

)

Upvotes: 1

HolgerBarlt
HolgerBarlt

Reputation: 317

Im not sure if this is exactly what you ment since the result you try to get has eighter some tipos in it or does not make sense to me, but have a look at this:

expDate <- function(startTime,t_par,t_conc){
  if(is.character(startTime)){
    startTime <- as.POSIXct(startTime,origin= "1970-01-01")
  }

  nExp <- 24
  timeExpand <- as.difftime(0:nExp,units="hours")
  returnDF <- data.frame(
    Date      = startTime + timeExpand,
    Parameter = rep(t_par,nExp+1),
    conc      = rep(t_conc,nExp+1)
  )
  return(returnDF)
}

tempData <- apply(obs,1,function(x) expDate(x[1],x[2],x[3]))
newData <- data.frame()
for(i in 1:length(tempData)){
  newData <- rbind(newData,tempData[[i]])
}

Upvotes: 0

Related Questions