gpt4 book ai didi

r - 无法取消嵌套具有不同列类型的列表数据框

转载 作者:行者123 更新时间:2023-12-04 01:36:54 30 4
gpt4 key购买 nike

我正在从封装在 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
.
.
.

最佳答案

一个选项是转换为通用类型,然后执行 unnest ,然后用 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>

或使用 type_convert来自 readr

关于r - 无法取消嵌套具有不同列类型的列表数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59204696/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com