I am new to purrr and struggling to understand how to append the result of my function onto my dataframe (and get the best performance, since my dataframe is large).
I'm attempting to calculate sunrise time for each row in a dataframe:
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])
}
I got this far, which gets me the desired sunrise times:
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"
But I can't seem to figure out how to get the results of my function appended on to the end of the "test" dataframe, say as another variable called "sunrise_time"...
test %>%
mutate(sunrisetime = pwalk(sunRise))
Error in mutate_impl(.data, dots) : Evaluation error: argument ".f" is missing, with no default.
Sidebar: if you can recommend a good purrr tutorial that worked for you, please include it in your answer!! There seems to be a lot to know about purrr and I'm not sure what to focus on as a first-timer.