0

I have dataframe A

structure(list(gauge = c(1094000L, 1094000L, 1094000L, 1100600L
), start = structure(c(2L, 4L, 1L, 3L), .Label = c("4/16/2007 18:45", 
"4/2/2004 12:45", "4/24/2012 9:15", "5/14/2006 21:00"), class = "factor")), .Names = c("gauge", 
"start"), row.names = c(NA, 4L), class = "data.frame")

And another dataframe B with timestamps

    structure(list(Time = structure(1:20, .Label = c("2002-01-01 00:00", 
"2002-01-01 00:05", "2002-01-01 00:10", "2002-01-01 00:15", "2002-01-01 00:20", 
"2002-01-01 00:25", "2002-01-01 00:30", "2002-01-01 00:35", "2002-01-01 00:40", 
"2002-01-01 00:45", "2002-01-01 00:50", "2002-01-01 00:55", "2002-01-01 01:00", 
"2002-01-01 01:05", "2002-01-01 01:10", "2002-01-01 01:15", "2002-01-01 01:20", 
"2002-01-01 01:25", "2002-01-01 01:30", "2002-01-01 01:35"), class = "factor"), 
    Precip.mm.h..1. = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0)), .Names = c("Time", "Precip.mm.h..1."
), row.names = c(NA, 20L), class = "data.frame")

I want to

  1. Read the time from A
  2. Find the exact time in B and it's row number
  3. Find corresponding Precip value in B

Using lubridate, I converted timestamp in A to UTC:

A$startTime <-mdy_hm(A$start)

But I am not sure how to compare two timestamps.

P.S. B is a really large file with a million rows. So I wasn't able to give you corresponding data for A.

maximusdooku
  • 5,242
  • 10
  • 54
  • 94
  • 1
    It will just give `NA`s without the corresponding data, but you need to parse your dates and join. With lubridate and base R, `A$start <- mdy_hm(A$start) ; B$Time <- ymd_hm(B$Time) ; merge(A, B, by.x = 'start', by.y = 'Time', all.x = TRUE)` – alistaire Oct 04 '16 at 21:10
  • @alistaire Why use base R instead of the dplyr solution you gave earlier? Just want to know pros and cons (speed etc.). – maximusdooku Oct 04 '16 at 21:18
  • I changed it mostly just to keep it basic; in this particular case the dplyr is not really easier to read. For parsing the dates, the time will be taken by lubridate, not the assignment, so the grammar doesn't matter (though `lubridate::parse_date_time2` may give you a speed boost). `dplyr::left_join` will likely be faster than `merge`, though. data.table will likely be faster yet if speed is a significant concern, though I find it a little harder to read quickly. – alistaire Oct 04 '16 at 23:24

0 Answers0