0
test1 <- structure(list(trip_count = 1:10, pickup_datetime = structure(c(1357019059, 
1357019939, 1357022493, 1357023065, 1357024439, 1357025235, 1357026348, 
1357026924, 1357027562, 1357028863), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), dropoff_datetime = structure(c(1357019158, 1357021384, 
1357023008, 1357024189, 1357024694, 1357025815, 1357026604, 1357027240, 
1357027830, 1357029381), tzone = "UTC", class = c("POSIXct", 
"POSIXt"))), row.names = c(NA, -10L), class = c("tbl_df", "tbl", 
"data.frame"))
test2 <- structure(list(DATE = structure(c(1357001460, 1357005060, 1357008660, 
1357012260, 1357015860, 1357019460, 1357023060, 1357026660, 1357030260, 
1357033860), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    HourlyDryBulbTemperature = c(39L, 38L, 39L, 39L, 39L, 39L, 
    39L, 38L, 39L, 39L), HourlyPrecipitation = c(0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0)), row.names = c(NA, 10L), class = "data.frame")

大家好,所以我有两个数据框,我想根据日期和时间加入它们。如果from是 inside和from ,它应该是一个fuzzy_join这样的连接工作。我试过了DATEtest2pickup_datetimedropoff_datetimetest1

test1 <- fuzzy_left_join(test1,test2,by = c("DATE" = "pickup_datetime", "DATE" = "dropoff_datetime"),match_fun = list(`>=`, `<=`))

但这会返回:Error: All columns in a tibble must be vectors. Column "col" is NULL.

更新:我找到了解决方案

dropoff_data <- str_split_fixed(test1$dropoff_datetime, " ", 2)
colnames(dropoff_data) <- c("join_date","dropoff_time")
test1 <- cbind.data.frame(test1,dropoff_data)
test1$join_time <- hour(hms(as.character(test1$dropoff_time)))
rm(dropoff_data)

dropoff_data <- str_split_fixed(test2$DATE, " ", 2)
colnames(dropoff_data) <- c("join_date","time")
test2 <- cbind.data.frame(test2,dropoff_data)
test2$join_time <- hour(hms(as.character(test2$time)))
rm(dropoff_data)

test1 <- left_join(test1,test2,by = c("join_date","join_time"))

感谢大家!

4

1 回答 1

0

可能是这样的东西可以帮助你:

library(data.table)
setDT(test1)
setDT(test2)
t <- test1[, c(.SD, as.list(test2)), by = 1:nrow(test1)]
t[DATE >= pickup_datetime & DATE <= dropoff_datetime]
于 2020-11-26T14:39:29.247 回答