提取时间最小的日期时间值R
Extract datetime value where time is minimal R
我正在使用 suncalc 包为一组具有 lat/lon 坐标的日期生成日照时间。这是一些示例代码:
library(tidyverse)
library(hms)
library(lubridate)
library(suncalc)
data <- structure(list(date = structure(c(17975, 18044, 18231, 18172,
18169), class = "Date"), lat = c(40.7128, 41.8781, 34.0522, 25.7617,
39.9526), lon = c(-74.006, -87.6298, -118.2437, -80.1918, -75.1652
)), class = "data.frame", row.names = c(NA, -5L))
data
date lat lon
1 2019-03-20 40.7128 -74.0060
2 2019-05-28 41.8781 -87.6298
3 2019-12-01 34.0522 -118.2437
4 2019-10-03 25.7617 -80.1918
5 2019-09-30 39.9526 -75.1652
从这里开始,生成给定时区每天的日出和日落时间相当简单:
data %>%
getSunlightTimes(data = ., keep = c("sunrise", "sunset"), tz = "America/Los_Angeles")
date lat lon sunrise sunset
1 2019-03-20 40.7128 -74.0060 2019-03-20 04:01:11 2019-03-20 16:08:32
2 2019-05-28 41.8781 -87.6298 2019-05-28 03:21:36 2019-05-28 18:16:52
3 2019-12-01 34.0522 -118.2437 2019-12-01 06:41:42 2019-12-01 16:45:14
4 2019-10-03 25.7617 -80.1918 2019-10-03 04:14:55 2019-10-03 16:07:12
5 2019-09-30 39.9526 -75.1652 2019-09-30 03:56:49 2019-09-30 15:47:06
生成的日出和日落值是 class POSIXct,同时表示日期和时间信息。从这里开始,我想要做的是提取时间最短的日出值,而不管日期如何。因此,在这种情况下我想要提取的值是 sunrise 列第 2 行中的数据。结果应包含完整的日期和时间信息,但该值是根据时间标准而非日期选择的。
如有任何帮助,我们将不胜感激!
转换为ITime
后我们可以使用slice
library(data.table)
library(dplyr)
data %>%
getSunlightTimes(data = ., keep = c("sunrise", "sunset"),
tz = "America/Los_Angeles") %>%
slice(which.min(as.ITime(sunrise)))
# date lat lon sunrise sunset
#1 2019-05-28 41.8781 -87.6298 2019-05-28 03:21:36 2019-05-28 18:16:52
或者使用 base R
,如果我们 format
,我们可以更容易地做到这一点,将 'Date' 部分更改为通用日期并使用 which.min
data[with(data, which.min(as.POSIXct(format(sunrise, "2020-01-01 %H:%M:%S")))),]
# date lat lon sunrise sunset
#2 2019-05-28 41.8781 -87.6298 2019-05-28 03:21:36 2019-05-28 18:16:52
在基数 R 中:
data$time_only <- strftime(df$sunrise, format="%H:%M:%S", tz = "America/Los_Angeles")
data[as.character(data$time_only) == min(data$time_only), -ncol(data)]
输出
# date lat lon sunrise sunset
# 2019-05-28 41.8781 -87.6298 2019-05-28 03:21:36 2019-05-28 18:16:52
我正在使用 suncalc 包为一组具有 lat/lon 坐标的日期生成日照时间。这是一些示例代码:
library(tidyverse)
library(hms)
library(lubridate)
library(suncalc)
data <- structure(list(date = structure(c(17975, 18044, 18231, 18172,
18169), class = "Date"), lat = c(40.7128, 41.8781, 34.0522, 25.7617,
39.9526), lon = c(-74.006, -87.6298, -118.2437, -80.1918, -75.1652
)), class = "data.frame", row.names = c(NA, -5L))
data
date lat lon
1 2019-03-20 40.7128 -74.0060
2 2019-05-28 41.8781 -87.6298
3 2019-12-01 34.0522 -118.2437
4 2019-10-03 25.7617 -80.1918
5 2019-09-30 39.9526 -75.1652
从这里开始,生成给定时区每天的日出和日落时间相当简单:
data %>%
getSunlightTimes(data = ., keep = c("sunrise", "sunset"), tz = "America/Los_Angeles")
date lat lon sunrise sunset
1 2019-03-20 40.7128 -74.0060 2019-03-20 04:01:11 2019-03-20 16:08:32
2 2019-05-28 41.8781 -87.6298 2019-05-28 03:21:36 2019-05-28 18:16:52
3 2019-12-01 34.0522 -118.2437 2019-12-01 06:41:42 2019-12-01 16:45:14
4 2019-10-03 25.7617 -80.1918 2019-10-03 04:14:55 2019-10-03 16:07:12
5 2019-09-30 39.9526 -75.1652 2019-09-30 03:56:49 2019-09-30 15:47:06
生成的日出和日落值是 class POSIXct,同时表示日期和时间信息。从这里开始,我想要做的是提取时间最短的日出值,而不管日期如何。因此,在这种情况下我想要提取的值是 sunrise 列第 2 行中的数据。结果应包含完整的日期和时间信息,但该值是根据时间标准而非日期选择的。
如有任何帮助,我们将不胜感激!
转换为ITime
slice
library(data.table)
library(dplyr)
data %>%
getSunlightTimes(data = ., keep = c("sunrise", "sunset"),
tz = "America/Los_Angeles") %>%
slice(which.min(as.ITime(sunrise)))
# date lat lon sunrise sunset
#1 2019-05-28 41.8781 -87.6298 2019-05-28 03:21:36 2019-05-28 18:16:52
或者使用 base R
,如果我们 format
,我们可以更容易地做到这一点,将 'Date' 部分更改为通用日期并使用 which.min
data[with(data, which.min(as.POSIXct(format(sunrise, "2020-01-01 %H:%M:%S")))),]
# date lat lon sunrise sunset
#2 2019-05-28 41.8781 -87.6298 2019-05-28 03:21:36 2019-05-28 18:16:52
在基数 R 中:
data$time_only <- strftime(df$sunrise, format="%H:%M:%S", tz = "America/Los_Angeles")
data[as.character(data$time_only) == min(data$time_only), -ncol(data)]
输出
# date lat lon sunrise sunset
# 2019-05-28 41.8781 -87.6298 2019-05-28 03:21:36 2019-05-28 18:16:52