【发布时间】:2021-08-18 17:50:13
【问题描述】:
我有一个由 4 列组成的数据框,其中包含来自 3 个不同位置、来自不同时期的温度数据 rbind - 在单个数据框中。我想从 3 个站点中选择 温度,它们是共同的日期/时间(小时)。
下面我提供了一个可重现的例子:
a1 <- seq.POSIXt(as.POSIXct("1995-01-01"), as.POSIXct("2007-04-01"), by = "120 min")
a2 <- seq.POSIXt(as.POSIXct("1998-04-19"), as.POSIXct("2004-03-20"), by = "60 min")
a3 <- seq.POSIXt(as.POSIXct("1991-01-01"), as.POSIXct("2001-04-01"), by = "180 min")
t1 <- runif(length(a1), min = -5, max = 45)
t2 <- runif(length(a2), min = -5, max = 45)
t3 <- runif(length(a3), min = -5, max = 45)
station1 <- data.frame(date = a1, temp = t1, ID = "station1")
station2 <- data.frame(date = a2, temp = t2, ID = "station2")
station3 <- data.frame(date = a3, temp = t3, ID = "station3")
all_stat <- rbind(station1,station2,station3)
all_stat <- all_stat %>%
mutate(time = hms::as_hms(date),
date = as_date(date)) %>%
relocate(date, time)
理想情况下,我希望在这 3 个站点中拥有 仅 临时数据的常见日期/小时的四列数据框(日期/时间/临时/ID)。我用dplyr 和subset 尝试了多种方法,但没有任何效果。
【问题讨论】: