您可以使用seq.POSIXt
組合來創建,無失的時間步長(對象grid.
)一data.frame
,然後用merge
在我的例子與觀察df
結合。
這應該解決您的問題
# Create a sample data.frame missing every second observation.
df <- data.frame(date=seq.POSIXt(from=as.POSIXct("1970-01-01 00:00:00"), to=as.POSIXct("1970-01-01 10:00:00"), by="2 hours"), rainfall=rnorm(6))
#Create a seq of times without anything missing
grid. <- data.frame(date=seq.POSIXt(as.POSIXct("1970-01-01 00:00:00"), to=as.POSIXct("1970-01-01 10:00:00"), by="1 hours"))
# Merge them together keeping all the values from grid.
dat. <- merge(grid., df, by="date", all.x=TRUE)
要刪除重複值,你可以找他們,並使用duplicated
功能刪除它們。
# The ! means the reverse logic. Therefore TRUE becomes FALSE.
dup_index <- !duplicated(dat.[,1])
# Now re-create the dat. object with only non-duplicated rows.
dat. <- dat.[dup_index,]
另一種方法是使用aggregate
函數。如果你有兩個實際上是兩個不同的觀察結果的副本,那麼這可能很有用,因此你需要這兩個平均值;
dat. <- aggregate(dat.[,2], by=list(dat[,1]), FUN=mean)
HTH