此帖子是从更早的帖子中解决的。我的新问题是如何正确使用pmap
。我传递了列表参数和函数。
我想通过:
day = 1
month = 11
year = 2015
为了更进一步,我想传递在这里获得的值:
library(lubridate)
seq.Date(from = as.Date("2015-01-01"), to = as.Date("2015-02-01"), by = "day") %>%
tibble::enframe(., name = "seq", value = "date") %>%
mutate(year = year(date),
month = month(date),
day = day(date))
# A tibble: 32 x 5
seq date year month day
<int> <date> <dbl> <dbl> <int>
1 1 2015-01-01 2015 1 1
2 2 2015-01-02 2015 1 2
3 3 2015-01-03 2015 1 3
4 4 2015-01-04 2015 1 4
5 5 2015-01-05 2015 1 5
6 6 2015-01-06 2015 1 6
7 7 2015-01-07 2015 1 7
8 8 2015-01-08 2015 1 8
9 9 2015-01-09 2015 1 9
10 10 2015-01-10 2015 1 10
功能:
Temps <- function(day, month, year){
month = stringr::str_pad(month, width = 2, pad = 0)
day = stringr::str_pad(day, width = 2, pad = 0)
url <- paste("https://www.timeanddate.com/weather/spain/madrid/historic?hd=", year, month, day, sep = "")
temps_obtained <- url %>%
html_session() %>%
read_html() %>%
html_table(fill = TRUE) %>%
.[[2]] %>%
setNames(.[1,]) %>%
as_tibble(., .name_repair = "universal") %>%
dplyr::slice(., -1) %>%
dplyr::slice(., -n())
return(temps_obtained)
}
purrr::pmap(list(1, 11, 2015), .f = Temps())
答案 0 :(得分:3)
如果我们使用的是pmap
,请确保将list
参数命名为与函数的参数匹配
purrr::pmap(list(day = 1, month = 11, year = 2015), Temps)
或者,如果我们传递一个未命名的list
,则用..
提取每个组件
purrr::pmap(list(1, 11, 2015), ~ Temps(day = ..1, month = ..2, year = ..3))