简体   繁体   English

如何提取与特定日期匹配的值?

[英]How to extract values that match certain date?

I have two text files: 我有两个文本文件:

1- first file 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- second file 2秒文件

and here is the second file: wg= [1] "2006/12/27 14:23:59" "2006/12/27 16:47:59" "2006/12/27 19:12:00" 这是第二个文件:wg = [1]“ 2006/12/27 14:23:59”“ 2006/12/27 16:47:59”“ 2006/12/27 19:12:00”

Here's a possible solution using data.table::foverlaps 这是使用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

Data 数据

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM