如何在最近的日期合并2个zoo/data.frame对象?
假设我有以下2个data.frames/zoo对象(您认为更容易使用的对象): 注意:我更喜欢base-R实现如何在最近的日期合并2个zoo/data.frame对象?,r,dataframe,zoo,R,Dataframe,Zoo,假设我有以下2个data.frames/zoo对象(您认为更容易使用的对象): 注意:我更喜欢base-R实现 一种基本R方法是 # Converting date column into date format. lookup.df[,"date"] <- as.Date(lookup.df[,"date"],"%m/%d/%Y") main.df[,"date"] <- as.Date(main.df[,"date"],"%m/%d/%Y") # Finding the ind
一种
基本R
方法是
# Converting date column into date format.
lookup.df[,"date"] <- as.Date(lookup.df[,"date"],"%m/%d/%Y")
main.df[,"date"] <- as.Date(main.df[,"date"],"%m/%d/%Y")
# Finding the index number under the defined condition.
index <- sapply(1:nrow(main.df), function(i){
diff <- as.numeric(main.df[i,"date"] - lookup.df[,"date"])
diff[diff<=0] <-NA
which.min(diff)
})
out <- data.frame(main.df,lookup.df[index,])
out[,c(1,3,2,4)]
从基R使用
findInterval
在lookup.df
中查找每个main.df
日期
findInterval
如果更改为NA
的第二行中没有匹配的间隔so,则返回0,以便后续行返回此类值的NA,而不是删除它们
请注意,lookup.df
在问题中是按日期顺序排序的,我们假设总是这样。如果不是,则首先对lookup.df进行排序
ix <- findInterval(main.df$date, lookup.df$date)
ix[ix == 0] <- NA
cbind(main = main.df, lookup = lookup.df[ix, ])
我第一次看到
findInterval()
。。。好捷径!我的向上投票。添加了与此答案不匹配的处理,因为这有时发生在真实数据集中。
# Converting date column into date format.
lookup.df[,"date"] <- as.Date(lookup.df[,"date"],"%m/%d/%Y")
main.df[,"date"] <- as.Date(main.df[,"date"],"%m/%d/%Y")
# Finding the index number under the defined condition.
index <- sapply(1:nrow(main.df), function(i){
diff <- as.numeric(main.df[i,"date"] - lookup.df[,"date"])
diff[diff<=0] <-NA
which.min(diff)
})
out <- data.frame(main.df,lookup.df[index,])
out[,c(1,3,2,4)]
date date.1 value value.1
1 1999-01-10 1999-01-01 10 1
1.1 1999-02-01 1999-01-01 20 1
3 1999-03-10 1999-03-08 30 3
3.1 1999-04-02 1999-03-08 40 3
4 1999-06-01 1999-04-05 50 4
ix <- findInterval(main.df$date, lookup.df$date)
ix[ix == 0] <- NA
cbind(main = main.df, lookup = lookup.df[ix, ])
main.date main.value lookup.date lookup.value
1 1999-01-10 10 NA NA
1.1 1999-02-01 20 1999-01-01 1
3 1999-03-10 30 1999-03-08 3
3.1 1999-04-02 40 1999-03-08 3
4 1999-06-01 50 1999-04-05 4