我有60天内每小时记录的GPS点(纬度&长度),我有同一时间段的日出和日落时间。我试图合并这两个数据集,这样我就可以知道当GPS点x被记录时,太阳是上升还是下降。
我已经设法使它工作,如果 sun
数据框小于 gps
数据框架。但如果 sun
的数据帧更大(如下面的例子所示),我得到一个错误。不幸的是,我需要代码在两种情况下都能工作(即,不管哪个数据框更大)。
示例数据和代码。
library(lubridate)
gps <- data.frame(lat = c(54.008, 54.009, 54.009, 54.008, 54.009, 54.009),
long = c(38.050, 38.051, 38.053, 38.050, 38.051, 38.053),
date = as.Date(c("2019-12-19", "2019-12-19", "2019-12-19", "2019-12-19","2019-12-20", "2019-12-20"), format = "%Y-%m-%d"),
time = as.numeric(hm("06:00", "07:00", "12:30", "13:00", "07:00", "24:00")))
sun <- data.frame(date = as.Date(c("2019-12-19", "2019-12-20", "2019-12-21", "2019-12-22", "2019-12-23", "2019-12-24", "2019-12-25"), format = "%Y-%m-%d"),
sunrise = as.numeric(hm("06:04", "06:05","06:06", "06:07","06:08", "06:09", "06:09")),
sunset = as.numeric(hm("23:06", "23:06","23:06", "23:06","23:06", "23:06", "23:08")))
gps$sunlight <- ifelse(sun$date == gps$date | sun$sunrise >= gps$time | sun$sunset <= gps$time, "N", "Y")
错误:
Error in `$<-.data.frame`(`*tmp*`, sunlight, value = c("N", "Y", "Y", :
replacement has 7 rows, data has 6
想要的输出。
> gps
lat long date time sunlight
1 54.008 38.050 2019-12-19 21600 N
2 54.009 38.051 2019-12-19 25200 Y
3 54.009 38.053 2019-12-19 45000 Y
4 54.008 38.050 2019-12-19 46800 Y
5 54.009 38.051 2019-12-20 25200 Y
6 54.009 38.053 2019-12-20 86400 N
任何建议或想法,我可能在哪里出错?
你需要先合并两个数据帧。用左连接每一行的 gps
将会尝试着与 sun
由 date
列。然后你可以应用条件 sunlight_cond
看看是否有GPS time
是介于 sunrise
和 sunset
. 然后,你可以将 TRUE
和 FALSE
值到所需的标签。Y
和 N
library(tidyverse)
gps %>%
left_join(sun, by="date") %>%
mutate(sunlight_cond = time >= sunrise & time <= sunset,
sunlight = factor(sunlight_cond, levels = c(TRUE, FALSE), labels = c("Y", "N")))
结果:
lat long date time sunrise sunset sunlight_cond sunlight
1 54.008 38.050 2019-12-19 21600 21840 83160 FALSE N
2 54.009 38.051 2019-12-19 25200 21840 83160 TRUE Y
3 54.009 38.053 2019-12-19 45000 21840 83160 TRUE Y
4 54.008 38.050 2019-12-19 46800 21840 83160 TRUE Y
5 54.009 38.051 2019-12-20 25200 21900 83160 TRUE Y
6 54.009 38.053 2019-12-20 86400 21900 83160 FALSE N