如何提取符合特定日期的值?

时间:2015-05-10 11:17:11

标签: r

我有两个文本文件:

1-第一个文件

 wd <- structure(list(Year = c(2006L, 2006L, 2006L), day = c(361L, 361L, 
360L), hour = c(14L, 8L, 8L), mint = c(30L, 0L, 30L), valu1 = c(0.5, 
0.3, 0.4)), .Names = c("Year", "day", "hour", "mint", "valu1"
), class = "data.frame", row.names = c(NA, -3L))
transform(wd, 
 Date = as.POSIXct(paste(Year, day, hour, mint), format = "%Y %j %H %M", tz = "UTC")
)

  Year day hour mint valu1                Date
 1 2006 361   14   30   0.5 2006-12-27 14:30:00
 2 2006 361    8    0   0.3 2006-12-27 08:00:00
 3 2006 360    8   30   0.4 2006-12-26 08:30:00

2-秒文件

这是第二个文件:     wg = [1]“2006/12/27 14:23:59”“2006/12/27 16:47:59”“2006/12/27 19:12:00”

1 个答案:

答案 0 :(得分:4)

以下是使用data.table::foverlaps

的可能解决方案
library(data.table)

## Convert `wg` to `POSIXct` class and make a `data.table` object out of it with start and end values
wg <- as.POSIXct(wg, format = "%Y/%m/%d %T", tz = "UTC") # Don't forget the time zone !
WG <- data.table(start = wg, end = wg)

## Key by start and end
setkey(WG)

## Do the same for `wd` adding +/- 30 minutes 
setDT(wd)[, `:=`(start = Date - 1800L, end = Date + 1800L)]


## Run foverlaps and extract the match `valu1` column
foverlaps(wd, WG, nomatch = 0L)[, .(wdDate = Date, valu1, WGDate = start)]
#                 wdDate valu1              WGDate
# 1: 2006-12-27 14:30:00   0.5 2006-12-27 14:23:59

数据

wd <- structure(list(Year = c(2006L, 2006L, 2006L), day = c(361L, 361L, 
360L), hour = c(14L, 8L, 8L), mint = c(30L, 0L, 30L), valu1 = c(0.5, 
0.3, 0.4), Date = structure(c(1167229800, 1167206400, 1167121800
), class = c("POSIXct", "POSIXt"), tzone = "UTC")), .Names = c("Year", 
"day", "hour", "mint", "valu1", "Date"), row.names = c(NA, -3L
), class = "data.frame")

wg <- c("2006/12/27 14:23:59", "2006/12/27 16:47:59", "2006/12/27 19:12:00")