我希望对R中的多个表的原始5到1秒间隔内插词“date”,“time”和“temp”进行内插:
旧:
date time temp
1 22.05.11 16:00:00 23.653
2 22.05.11 16:00:05 23.541
...
新:
date time temp
1 22.05.11 16:00:00 23.653
2 22.05.11 16:00:01 23.631
3 22.05.11 16:00:02 23.609
...
我该怎么做? 谢谢你的帮助。
答案 0 :(得分:7)
使用na.approx
包中的na.spline
和/或zoo
很容易做到这一点。
# example data
set.seed(21)
z <- zoo(23+runif(10), seq(Sys.time(),length.out=10,by=5))
# merge your data with an empty zoo object that has an index value for
# every period you're interested in.
y <- merge(z, zoo(order.by=seq(start(z), end(z), by=1)))
xa <- na.approx(y)
xs <- na.spline(y)
plot(merge(xa,xs))
# To convert your existing data.frame to a zoo object:
z <- zoo(Data$temp,
as.POSIXct(paste(Data$date, Data$time), format="%d.%m.%y %H:%M:%S"))
y <- merge(z, zoo(order.by=seq(start(z), end(z), by=1)))
xa <- na.approx(y)
xs <- na.spline(y)
plot(merge(xa,xs))
# Convert back to data.frame
dfxa <- data.frame(date=format(index(xa), "%d.%m.%y"),
time=format(index(xa), "%H:%M:%S"), temp=coredata(xa))