I think this might do what you want. I am not sure why the final merged dataset begins at 3:00PM on Dec 31 instead of midnight Jan 1st. I suspect that has something to do with my computer's clock relative to GMT.
df.1 <- read.table(text = '
date time station210
1994-01-01 00:00:00 0
1994-01-01 02:00:00 0
1994-01-01 03:00:00 0
1994-01-01 04:00:00 0.6
1994-01-01 06:00:00 2.6
1994-01-01 07:00:00 3.2
', header = TRUE, stringsAsFactors=FALSE)
df.2 <- read.table(text = '
date time station212
1994-01-01 00:00:00 0
1994-01-01 01:00:00 1.8
1994-01-01 02:00:00 1.8
1994-01-01 03:00:00 1.8
1994-01-01 04:00:00 1.4
1994-01-01 06:00:00 1.8
', header=TRUE, stringsAsFactors=FALSE)
cols <- c( 'date' , 'time' )
df.1$datetime <- apply( df.1[ , cols ] , 1 , paste , collapse = " " )
df.2$datetime <- apply( df.2[ , cols ] , 1 , paste , collapse = " " )
df.1 <- df.1[, c('datetime', 'station210')]
df.2 <- df.2[, c('datetime', 'station212')]
df.3 <- merge(df.1, df.2, by="datetime", all=TRUE)
df.3[order(df.3$datetime),]
df.3$datetime <- format(as.POSIXct(df.3$datetime, format = "%Y-%m-%d %H:%M:%S"), "%Y-%m-%d %H:%M:%S" )
df.3
hour <- seq(0,60*60*24,by=60*60)
datetime <- as.POSIXlt(hour, origin="1994-01-01")
datetime <- format( as.POSIXct(hour, origin="1994-01-01"), "%Y-%m-%d %H:%M:%S" )
newdf <- merge(data.frame(datetime), df.3, all.x=TRUE, by="datetime")
newdf
datetime station210 station212
1 1993-12-31 15:00:00 NA NA
2 1993-12-31 16:00:00 NA NA
3 1993-12-31 17:00:00 NA NA
4 1993-12-31 18:00:00 NA NA
5 1993-12-31 19:00:00 NA NA
6 1993-12-31 20:00:00 NA NA
7 1993-12-31 21:00:00 NA NA
8 1993-12-31 22:00:00 NA NA
9 1993-12-31 23:00:00 NA NA
10 1994-01-01 00:00:00 0.0 0.0
11 1994-01-01 01:00:00 NA 1.8
12 1994-01-01 02:00:00 0.0 1.8
13 1994-01-01 03:00:00 0.0 1.8
14 1994-01-01 04:00:00 0.6 1.4
15 1994-01-01 05:00:00 NA NA
16 1994-01-01 06:00:00 2.6 1.8
17 1994-01-01 07:00:00 3.2 NA
18 1994-01-01 08:00:00 NA NA
19 1994-01-01 09:00:00 NA NA
20 1994-01-01 10:00:00 NA NA
21 1994-01-01 11:00:00 NA NA
22 1994-01-01 12:00:00 NA NA
23 1994-01-01 13:00:00 NA NA
24 1994-01-01 14:00:00 NA NA
25 1994-01-01 15:00:00 NA NA
EDIT - July 6, 2013
Here is one way to handle more than two data frames.
Here are the data:
df.1 <- read.table(text = '
date time station210
1994-01-01 00:00:00 0
1994-01-01 02:00:00 0
1994-01-01 03:00:00 0
1994-01-01 04:00:00 0.6
1994-01-01 06:00:00 2.6
1994-01-01 07:00:00 3.2
', header = TRUE, stringsAsFactors=FALSE)
df.2 <- read.table(text = '
date time station212
1994-01-01 00:00:00 0
1994-01-01 01:00:00 1.8
1994-01-01 02:00:00 1.8
1994-01-01 03:00:00 1.8
1994-01-01 04:00:00 1.4
1994-01-01 06:00:00 1.8
', header=TRUE, stringsAsFactors=FALSE)
df.3 <- read.table(text = '
date time station214
1993-12-31 22:00:00 5.0
1993-12-31 23:00:00 2.0
1994-01-01 02:00:00 1.0
1994-01-01 04:00:00 3.0
1994-01-01 06:00:00 5.0
1994-01-01 08:00:00 4.0
', header=TRUE, stringsAsFactors=FALSE)
Create a list of data frames and create the variable datetime
:
my.data <- sapply(paste('df.', seq(1,3,1), sep=''), get, environment(), simplify = FALSE)
date.time <- function(x) {
cols <- c( 'date' , 'time' )
x$datetime <- apply( x[ , cols ] , 1 , paste , collapse = " " )
x <- x[, 3:4]
return(x)
}
my.list <- lapply(my.data, function(x) date.time(x))
Merge and sort the data frames in that list:
df.3 <- Reduce(function(...) merge(..., all=T), my.list)
df.3[order(df.3$datetime),]
Add missing dates and times to the merged data frame:
df.3$datetime <- format(as.POSIXct(df.3$datetime, format = "%Y-%m-%d %H:%M:%S"), "%Y-%m-%d %H:%M:%S" )
hour <- seq(0,60*60*24,by=60*60)
datetime <- as.POSIXlt(hour, origin="1994-01-01")
datetime <- format( as.POSIXct(hour, origin="1994-01-01"), "%Y-%m-%d %H:%M:%S" )
newdf <- merge(data.frame(datetime), df.3, all.x=TRUE, by="datetime")
newdf
Here is code to replace missing observations from a station with the mean of the preceding and following observations from that same station. I am using nested for-loops
which are likely highly inefficient. If I figure out a more efficient approach I will try to remember to post it here. If your data set is huge, these nested for-loops
may take a very long time to run.
newdf2 <- newdf
for(i in 1:nrow(newdf)) {
for(j in 2:ncol(newdf)) {
if(i == 1 & is.na(newdf[i,j])) newdf2[i,j] = newdf[i+1,j]
if(i == nrow(newdf) & is.na(newdf[i,j])) newdf2[i,j] = newdf[i-1,j]
if(i > 1 & i < nrow(newdf) & is.na(newdf[i,j])) newdf2[i,j] = mean(c(newdf[i-1,j], newdf[i+1,j]), na.rm=TRUE)
if(is.nan(newdf2[i,j])) newdf2[i,j] = NA
}
}
cbind(newdf, newdf2)