【发布时间】:2020-03-30 22:45:57
【问题描述】:
我从封装在 R 包中的 API 中提取一些道路交通数据。我正在使用列表数据框来控制多组记录的下载。
# install.packages(webTRISr)
library(webTRISr)
library(tidyverse)
sites <- c(5745, 6345)
start_date = '01112017'
end_date = '31122017'
road_reports <- data_frame(sites, start_date, end_date) %>%
mutate(data = purrr::pmap(list(sites, start_date, end_date), webTRISr::webtris_report, report_type = "daily"))
当我来到unnest 结果...
road_reports %>%
unnest(data)
# Error: No common type for `..1$data$Site Name` <character> and `..2$data$Site Name` <double>.
这是因为“站点名称”列在 API 的一次调用中是一个字符,但在另一个调用中是双精度。
从这个已关闭的tidyr 问题 (https://github.com/tidyverse/tidyr/issues/658) 我认为这已被视为错误并已在tidyr v1.0.0 中排序。
有什么解决办法吗? this SO answer 的解决方案给出了同样的错误。
我尝试将 ptype 参数传递给 unnest() 以强制数据类型,但得到有损转换错误,即:
ptype <- data_frame('Site Name'= character(),
'Report Date' = as.POSIXct(character(), tz = "UTC"),
'Time Period Ending' = hms::as_hms(character()),
'Time Interval' = double(),
'0 - 520 cm' = double(),
'521 - 660 cm' = double(),
'661 - 1160 cm' = double(),
'1160+ cm' = double(),
'0 - 10 mph' = logical(),
'11 - 15 mph' = logical(),
'16 - 20 mph' = logical(),
'21 - 25 mph' = logical(),
'26 - 30 mph' = logical(),
'31 - 35 mph' = logical(),
'36 - 40 mph' = logical(),
'41 - 45 mph' = logical(),
'46 - 50 mph' = logical(),
'51 - 55 mph' = logical(),
'56 - 60 mph' = logical(),
'61 - 70 mph' = logical(),
'71 - 80 mph' = logical(),
'80+ mph' = logical(),
'Avg mph' = double(),
'Total Volume' = double()
)
road_reports %>%
unnest(data, ptype = ptype)
#Error: Lossy cast from <data.frame<data:data.frame< Site Name : character Report Date : datetime<UTC> Time Period Ending: time Time Interval : double
.
.
.
【问题讨论】: