Skip to content
Prev 299024 / 398506 Next

insert missing dates

Hello,

The trick is to use seq() for date classes.
First of all, when creating data.frames use stringsAsFactors=FALSE, in 
order not to convert them to factors. I've added this option to your 
data.frame() instructions. And then,


mydata1$dates  <- as.POSIXct(mydata1$dates)
mydata1b$dates <- as.POSIXct(mydata1b$dates)
mydata2$dates  <- as.POSIXct(mydata2$dates)
mydata2b$dates <- as.POSIXct(mydata2b$dates)


Now, first we create the sequences then use merge(9, that will take care 
of inserting the NAs.

# mydata1
tmp <- tapply(mydata1$dates, format(mydata1$dates, "%Y-%m-%d"), function(x){
		if(length(x) < 2) paste(format(x, "%Y-%m-%d"), c("07:00:00", "19:00:00"))
		else sub("0 [[:alpha:]]+$", "", x)
	})
tmp <- as.POSIXct(unlist(tmp))
merge(mydata1, data.frame(dates=tmp), all.y=TRUE)

# and mydata2
tmp <- data.frame(dates=seq(mydata2$dates[1], mydata2$dates[n], by="10 
mins"))
merge(mydata2, tmp, all.y=TRUE)


Hope this helps,

Rui Barradas

Em 03-07-2012 10:52, ??????????? ????????? escreveu: