Reputation: 557
I'm pulling some road traffic data from an API wrapped in an R package. I'm using a list dataframe to control the download of multiple sets of records.
# 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"))
When I come to unnest
the results...
road_reports %>%
unnest(data)
# Error: No common type for `..1$data$Site Name` <character> and `..2$data$Site Name` <double>.
This is because the column 'Site Name' is a character in one call from the API, but is a double in another.
From this tidyr
issue, which has been closed (https://github.com/tidyverse/tidyr/issues/658) I thought this had been considered a bug and had been sorted in tidyr
v1.0.0.
Any ideas for a work around? The solution from this SO answer gives the same error.
I've tried passing a ptype
argument to unnest()
to coerce the data types but get a lossy cast error ie:
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
.
.
.
Upvotes: 5
Views: 1904
Reputation: 887118
An option is to convert to a common type, then do the unnest
, and later change the type with type.convert
library(purrr)
library(dplyr)
road_reports %>%
mutate(data = map(data, ~ .x %>%
mutate_all(as.character))) %>%
unnest(data) %>%
type.convert
# type.convert(., as.is = TRUE) # to avoid getting factor columns
# A tibble: 11,232 x 27
# sites start_date end_date `Site Name` `Report Date` `Time Period En… `Time Interval` `0 - 520 cm` `521 - 660 cm` `661 - 1160 cm` `1160+ cm`
# <int> <int> <int> <fct> <fct> <fct> <int> <int> <int> <int> <int>
# 1 5745 1112017 31122017 M1/5170L 2017-11-01 00:14:59 0 NA NA NA NA
# 2 5745 1112017 31122017 M1/5170L 2017-11-01 00:29:59 1 NA NA NA NA
# 3 5745 1112017 31122017 M1/5170L 2017-11-01 00:44:59 2 NA NA NA NA
# 4 5745 1112017 31122017 M1/5170L 2017-11-01 00:59:59 3 NA NA NA NA
# 5 5745 1112017 31122017 M1/5170L 2017-11-01 01:14:59 4 NA NA NA NA
# 6 5745 1112017 31122017 M1/5170L 2017-11-01 01:29:59 5 NA NA NA NA
# 7 5745 1112017 31122017 M1/5170L 2017-11-01 01:44:59 6 NA NA NA NA
# 8 5745 1112017 31122017 M1/5170L 2017-11-01 01:59:59 7 NA NA NA NA
# 9 5745 1112017 31122017 M1/5170L 2017-11-01 02:14:59 8 NA NA NA NA
#10 5745 1112017 31122017 M1/5170L 2017-11-01 02:29:59 9 NA NA NA NA
# … with 11,222 more rows, and 16 more variables: `0 - 10 mph` <int>, `11 - 15 mph` <int>, `16 - 20 mph` <int>, `21 - 25 mph` <int>, `26 - 30
# mph` <int>, `31 - 35 mph` <int>, `36 - 40 mph` <int>, `41 - 45 mph` <int>, `46 - 50 mph` <int>, `51 - 55 mph` <int>, `56 - 60 mph` <int>, `61 -
# 70 mph` <int>, `71 - 80 mph` <int>, `80+ mph` <int>, `Avg mph` <int>, `Total Volume` <int>
Or use type_convert
from readr
Upvotes: 6