使用 purrr 计算 R 中每一行的日出时间
Use purrr to calculate sunrise time for each row in R
我是 purrr 的新手,正在努力了解如何将我的函数的结果附加到我的数据帧上(并获得最佳性能,因为我的数据帧很大)。
我正在尝试计算数据框中每一行的日出时间:
library(tidyverse)
library(StreamMetabolism)
test <- structure(list(Latitude = c(44.49845, 42.95268, 42.95268, 44.49845,
44.49845, 44.49845), Longitude = c(-78.19259, -81.36935, -81.36935, -78.19259,
-78.19259, -78.19259), date = c("2014/02/12", "2014/01/24", "2014/01/08",
"2014/01/11", "2014/01/10", "2014/01/07"), timezone = c("EST5EDT", "EST5EDT",
"EST5EDT", "EST5EDT", "EST5EDT", "EST5EDT")), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -6L))
sunRise <- function(Latitude, Longitude, date, timezone){
print(sunrise.set(Latitude, Longitude, date, timezone, num.days = 1)[1,1])
}
我走到这一步,得到了我想要的日出时间:
test %>%
pwalk(sunRise)
[1] "2014-02-12 07:17:09 EST"
[1] "2014-01-24 07:47:55 EST"
[1] "2014-01-08 07:56:13 EST"
[1] "2014-01-11 07:47:38 EST"
[1] "2014-01-10 07:47:59 EST"
[1] "2014-01-07 07:48:48 EST"
但我似乎无法弄清楚如何将我的函数的结果附加到 "test" 数据帧的末尾,比如另一个名为 "sunrise_time" 的变量...
test %>%
mutate(sunrisetime = pwalk(sunRise))
Error in mutate_impl(.data, dots) : Evaluation error: argument ".f" is missing, with no default.
侧边栏: 如果您可以推荐一个适合您的 purrr 教程,请将其包含在您的回答中!!关于 purrr 似乎有很多东西需要了解,我不确定作为初学者应该关注什么。
这里你真的不需要purrr
。这是一个 dplyr
方法:
library(dplyr)
library(StreamMetabolism)
# updated function
sunRise <- function(Latitude, Longitude, date, timezone){
sunrise.set(Latitude, Longitude, date, timezone, num.days = 1)[1,1]
}
test %>%
rowwise() %>%
mutate(sunrize_time = sunRise(Latitude, Longitude, date, timezone)) %>%
ungroup()
# # A tibble: 6 x 5
# Latitude Longitude date timezone sunrize_time
# <dbl> <dbl> <chr> <chr> <dttm>
# 1 44.5 -78.2 2014/02/12 EST5EDT 2014-02-12 07:17:09
# 2 43.0 -81.4 2014/01/24 EST5EDT 2014-01-24 07:47:55
# 3 43.0 -81.4 2014/01/08 EST5EDT 2014-01-08 07:56:13
# 4 44.5 -78.2 2014/01/11 EST5EDT 2014-01-11 07:47:38
# 5 44.5 -78.2 2014/01/10 EST5EDT 2014-01-10 07:47:59
# 6 44.5 -78.2 2014/01/07 EST5EDT 2014-01-07 07:48:48
或者如果你想使用 purr
你可以这样做:
library(tidyverse)
test %>%
group_by(id = row_number()) %>%
nest() %>%
mutate(sunrise_time = map(data, ~sunRise(.x$Latitude, .x$Longitude, .x$date, .x$timezone))) %>%
unnest()
# # A tibble: 6 x 6
# id sunrise_time Latitude Longitude date timezone
# <int> <dttm> <dbl> <dbl> <chr> <chr>
# 1 1 2014-02-12 07:17:09 44.5 -78.2 2014/02/12 EST5EDT
# 2 2 2014-01-24 07:47:55 43.0 -81.4 2014/01/24 EST5EDT
# 3 3 2014-01-08 07:56:13 43.0 -81.4 2014/01/08 EST5EDT
# 4 4 2014-01-11 07:47:38 44.5 -78.2 2014/01/11 EST5EDT
# 5 5 2014-01-10 07:47:59 44.5 -78.2 2014/01/10 EST5EDT
# 6 6 2014-01-07 07:48:48 44.5 -78.2 2014/01/07 EST5EDT
如果需要,您可以删除 id
列。
或者,您可以稍微更改您的函数并执行以下操作:
# update function
sunRise <- function(Latitude, Longitude, date, timezone){
return(list(sunrise_time = sunrise.set(Latitude, Longitude, date, timezone, num.days = 1)[1,1]))
}
# apply function to each row and create a dataframe
# bind columns with original dataset
pmap_df(test, sunRise) %>%
cbind(test, .)
# Latitude Longitude date timezone sunrise_time
# 1 44.49845 -78.19259 2014/02/12 EST5EDT 2014-02-12 07:17:09
# 2 42.95268 -81.36935 2014/01/24 EST5EDT 2014-01-24 07:47:55
# 3 42.95268 -81.36935 2014/01/08 EST5EDT 2014-01-08 07:56:13
# 4 44.49845 -78.19259 2014/01/11 EST5EDT 2014-01-11 07:47:38
# 5 44.49845 -78.19259 2014/01/10 EST5EDT 2014-01-10 07:47:59
# 6 44.49845 -78.19259 2014/01/07 EST5EDT 2014-01-07 07:48:48
我喜欢@AntoniosK 的解决方案,但你非常接近。这有效,只要为自定义函数定义的变量都包含在数据框中:
test %>%
mutate(sunrise_time = pmap(., sunRise))
一个非常有用的 purrr
教程:Jenny Bryan Purrr Tutorial
我是 purrr 的新手,正在努力了解如何将我的函数的结果附加到我的数据帧上(并获得最佳性能,因为我的数据帧很大)。
我正在尝试计算数据框中每一行的日出时间:
library(tidyverse)
library(StreamMetabolism)
test <- structure(list(Latitude = c(44.49845, 42.95268, 42.95268, 44.49845,
44.49845, 44.49845), Longitude = c(-78.19259, -81.36935, -81.36935, -78.19259,
-78.19259, -78.19259), date = c("2014/02/12", "2014/01/24", "2014/01/08",
"2014/01/11", "2014/01/10", "2014/01/07"), timezone = c("EST5EDT", "EST5EDT",
"EST5EDT", "EST5EDT", "EST5EDT", "EST5EDT")), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -6L))
sunRise <- function(Latitude, Longitude, date, timezone){
print(sunrise.set(Latitude, Longitude, date, timezone, num.days = 1)[1,1])
}
我走到这一步,得到了我想要的日出时间:
test %>%
pwalk(sunRise)
[1] "2014-02-12 07:17:09 EST"
[1] "2014-01-24 07:47:55 EST"
[1] "2014-01-08 07:56:13 EST"
[1] "2014-01-11 07:47:38 EST"
[1] "2014-01-10 07:47:59 EST"
[1] "2014-01-07 07:48:48 EST"
但我似乎无法弄清楚如何将我的函数的结果附加到 "test" 数据帧的末尾,比如另一个名为 "sunrise_time" 的变量...
test %>%
mutate(sunrisetime = pwalk(sunRise))
Error in mutate_impl(.data, dots) : Evaluation error: argument ".f" is missing, with no default.
侧边栏: 如果您可以推荐一个适合您的 purrr 教程,请将其包含在您的回答中!!关于 purrr 似乎有很多东西需要了解,我不确定作为初学者应该关注什么。
这里你真的不需要purrr
。这是一个 dplyr
方法:
library(dplyr)
library(StreamMetabolism)
# updated function
sunRise <- function(Latitude, Longitude, date, timezone){
sunrise.set(Latitude, Longitude, date, timezone, num.days = 1)[1,1]
}
test %>%
rowwise() %>%
mutate(sunrize_time = sunRise(Latitude, Longitude, date, timezone)) %>%
ungroup()
# # A tibble: 6 x 5
# Latitude Longitude date timezone sunrize_time
# <dbl> <dbl> <chr> <chr> <dttm>
# 1 44.5 -78.2 2014/02/12 EST5EDT 2014-02-12 07:17:09
# 2 43.0 -81.4 2014/01/24 EST5EDT 2014-01-24 07:47:55
# 3 43.0 -81.4 2014/01/08 EST5EDT 2014-01-08 07:56:13
# 4 44.5 -78.2 2014/01/11 EST5EDT 2014-01-11 07:47:38
# 5 44.5 -78.2 2014/01/10 EST5EDT 2014-01-10 07:47:59
# 6 44.5 -78.2 2014/01/07 EST5EDT 2014-01-07 07:48:48
或者如果你想使用 purr
你可以这样做:
library(tidyverse)
test %>%
group_by(id = row_number()) %>%
nest() %>%
mutate(sunrise_time = map(data, ~sunRise(.x$Latitude, .x$Longitude, .x$date, .x$timezone))) %>%
unnest()
# # A tibble: 6 x 6
# id sunrise_time Latitude Longitude date timezone
# <int> <dttm> <dbl> <dbl> <chr> <chr>
# 1 1 2014-02-12 07:17:09 44.5 -78.2 2014/02/12 EST5EDT
# 2 2 2014-01-24 07:47:55 43.0 -81.4 2014/01/24 EST5EDT
# 3 3 2014-01-08 07:56:13 43.0 -81.4 2014/01/08 EST5EDT
# 4 4 2014-01-11 07:47:38 44.5 -78.2 2014/01/11 EST5EDT
# 5 5 2014-01-10 07:47:59 44.5 -78.2 2014/01/10 EST5EDT
# 6 6 2014-01-07 07:48:48 44.5 -78.2 2014/01/07 EST5EDT
如果需要,您可以删除 id
列。
或者,您可以稍微更改您的函数并执行以下操作:
# update function
sunRise <- function(Latitude, Longitude, date, timezone){
return(list(sunrise_time = sunrise.set(Latitude, Longitude, date, timezone, num.days = 1)[1,1]))
}
# apply function to each row and create a dataframe
# bind columns with original dataset
pmap_df(test, sunRise) %>%
cbind(test, .)
# Latitude Longitude date timezone sunrise_time
# 1 44.49845 -78.19259 2014/02/12 EST5EDT 2014-02-12 07:17:09
# 2 42.95268 -81.36935 2014/01/24 EST5EDT 2014-01-24 07:47:55
# 3 42.95268 -81.36935 2014/01/08 EST5EDT 2014-01-08 07:56:13
# 4 44.49845 -78.19259 2014/01/11 EST5EDT 2014-01-11 07:47:38
# 5 44.49845 -78.19259 2014/01/10 EST5EDT 2014-01-10 07:47:59
# 6 44.49845 -78.19259 2014/01/07 EST5EDT 2014-01-07 07:48:48
我喜欢@AntoniosK 的解决方案,但你非常接近。这有效,只要为自定义函数定义的变量都包含在数据框中:
test %>%
mutate(sunrise_time = pmap(., sunRise))
一个非常有用的 purrr
教程:Jenny Bryan Purrr Tutorial