r - 根据现有数据框为 read_csv 创建 col_types 字符串规范
问题描述
我有一个 data.frame 或 tibble,它在一个脚本中写入 CSV 文件。在另一个脚本中,相同的 CSV 文件被读入 data.frame 或 tibble。使用read_csv()
, 和col_types=
参数,我可以指定要读入的列类型。这是一个例子:
# Create an example dataframe
df <- tibble::tibble(a=1L
, b=1.0
, c="a"
, d=TRUE
, e=lubridate::ymd_hms("2019-03-19T13:15:18Z")
, f=lubridate::ymd("2019-03-19")
, g=factor("a"))
# Write csv to file
readr::write_csv(df, "temp.csv")
# read it back in, supplying a col_types string spec
readr::read_csv("temp.csv", col_types="idclTDf")
#> # A tibble: 1 x 7
#> a b c d e f g
#> <int> <dbl> <chr> <lgl> <dttm> <date> <fct>
#> 1 1 1 a TRUE 2019-03-19 13:15:18 2019-03-19 a
由reprex 包(v0.2.1)于 2019 年 3 月 19 日创建
问题是我需要知道函数的col_types=
参数read_csv()
(或者让它猜测,我不想这样做)。我想要的是某种方式来获取原始文件df
,并且在我写出来之前,从可用于读取转储的 CSVcol_types
的对象生成字符串df
。也就是说,我想要一些可以创建"idclTDf"
给定字符串的东西data.frame 作为参数。
我看到这里有一个功能请求(我已经加了两分钱):https ://github.com/tidyverse/readr/issues/895 。
解决方案
我确实有一个解决方案,并且它有效,但我认为它非常不完整并且没有硬化。这是我对解决方案的尝试。
# https://github.com/tidyverse/readr/issues/895
# Create function to take a tibble and return a character string that can be used in `readr::read_csv()`
# as the `col_types` argument to re-read this back into a dataframe after it had been written out
# by `write_csv()`.
get_col_types_short <- function(.df) {
# Get column classes from input dataframe
lst_col_classes__ <- purrr::map(.df, ~ class(.x))
# Map classes to known single-character col_types indicator
vl_col_class_char__ <- purrr::map_chr(lst_col_classes__, function(.e) {
dplyr::case_when(
"logical" %in% .e ~ "l"
, "integer" %in% .e ~ "i"
, "numeric" %in% .e ~ "d"
, "double" %in% .e ~ "d"
, "character" %in% .e ~ "c"
, "factor" %in% .e ~ "f"
, "Date" %in% .e ~ "D"
, "POSIXct" %in% .e ~ "T"
, TRUE ~ "c"
)
})
# Return vector of single-character col_type indicator.
# Element name is the source column it came from.
vl_col_class_char__
}
# Test it:
df <- tibble::tibble(a=1L
, b=1.0
, c="a"
, d=TRUE
, e=lubridate::ymd_hms("2019-03-19T13:15:18Z")
, f=lubridate::ymd("2019-03-19")
, g=factor("a"))
v__ <- get_col_types_short(df)
# Show what is actually returned
v__
#> a b c d e f g
#> "i" "d" "c" "l" "T" "D" "f"
# Collapse it to show how to use it
paste(v__, collapse="")
#> [1] "idclTDf"
# Write csv to file
readr::write_csv(df, "temp.csv")
# read it back in, using the above col_types string spec
readr::read_csv("temp.csv", col_types=paste(v__, collapse=""))
#> # A tibble: 1 x 7
#> a b c d e f g
#> <int> <dbl> <chr> <lgl> <dttm> <date> <fct>
#> 1 1 1 a TRUE 2019-03-19 13:15:18 2019-03-19 a
由reprex 包(v0.2.1)于 2019 年 3 月 19 日创建
推荐阅读
- php - Magento 2 Braintree Card Payment 与 react native 应用程序集成
- c++ - 在 C++ 的另一个函数中使用 main() 中的数组
- python - 使用 openpyxl 获取工作表的名称
- java - if 语句字节与整数
- object - 在 Logtalk 中将对象保存到文件
- angular - 如何使用量角器打字稿在具有跨度的按钮上单击具有相似元素详细信息的元素
- java - URLEncodedUtils 不解析来自给定 url 的所有获取参数
- java - leetcode 109:将排序列表转换为二叉搜索树
- java - NoClassDefFoundError: org/elasticsearch/script/ScriptEngine
- python - 根据 Python 中的唯一键将列中的类别转换为编码为 1 或 0 的多列