使用purrr计算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"

但我似乎不知道如何将函数的结果附加到"测试"数据帧的末尾,比如说作为另一个名为"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教程

相关内容

最新更新