1

I have two text files:

1- first file

 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

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"

sacvf
  • 2,463
  • 5
  • 36
  • 54
  • What you show are not text files. Are you showing how they look like after importing them into R? –  May 10 '15 at 11:26
  • 2
    [**This nice answer**](http://stackoverflow.com/questions/24480031/roll-join-with-start-end-window/25655497#25655497) should get you going. – Henrik May 10 '15 at 11:38

1 Answers1

4

Here's a possible solution using 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")
David Arenburg
  • 91,361
  • 17
  • 137
  • 196