这些函数使用 Arrow C++ CSV 阅读器将数据读取到 tibble 中。Arrow C++ 选项已映射到遵循 readr::read_delim() 的参数名称,而 col_select 的灵感来自 vroom::vroom()。
用法
read_delim_arrow(
file,
delim = ",",
quote = "\"",
escape_double = TRUE,
escape_backslash = FALSE,
schema = NULL,
col_names = TRUE,
col_types = NULL,
col_select = NULL,
na = c("", "NA"),
quoted_na = TRUE,
skip_empty_rows = TRUE,
skip = 0L,
parse_options = NULL,
convert_options = NULL,
read_options = NULL,
as_data_frame = TRUE,
timestamp_parsers = NULL,
decimal_point = "."
)
read_csv_arrow(
file,
quote = "\"",
escape_double = TRUE,
escape_backslash = FALSE,
schema = NULL,
col_names = TRUE,
col_types = NULL,
col_select = NULL,
na = c("", "NA"),
quoted_na = TRUE,
skip_empty_rows = TRUE,
skip = 0L,
parse_options = NULL,
convert_options = NULL,
read_options = NULL,
as_data_frame = TRUE,
timestamp_parsers = NULL
)
read_csv2_arrow(
file,
quote = "\"",
escape_double = TRUE,
escape_backslash = FALSE,
schema = NULL,
col_names = TRUE,
col_types = NULL,
col_select = NULL,
na = c("", "NA"),
quoted_na = TRUE,
skip_empty_rows = TRUE,
skip = 0L,
parse_options = NULL,
convert_options = NULL,
read_options = NULL,
as_data_frame = TRUE,
timestamp_parsers = NULL
)
read_tsv_arrow(
file,
quote = "\"",
escape_double = TRUE,
escape_backslash = FALSE,
schema = NULL,
col_names = TRUE,
col_types = NULL,
col_select = NULL,
na = c("", "NA"),
quoted_na = TRUE,
skip_empty_rows = TRUE,
skip = 0L,
parse_options = NULL,
convert_options = NULL,
read_options = NULL,
as_data_frame = TRUE,
timestamp_parsers = NULL
)参数
- file
字符文件名或 URI、连接、文字数据(单个字符串或 原始 向量)、Arrow 输入流,或带有路径的
FileSystem(SubTreeFileSystem)。如果是文件名,将打开一个内存映射的 Arrow InputStream 并在完成后关闭;将从文件扩展名检测并自动处理压缩。
要被识别为文字数据,输入必须用
I()包装。- delim
用于分隔记录中字段的单个字符。
- quote
用于引用字符串的单个字符。
- escape_double
文件是否通过双引号来转义引号?例如,如果此选项为
TRUE,则值""""表示单个引号,即\"。- escape_backslash
文件是否使用反斜杠来转义特殊字符?这比
escape_double更通用,因为反斜杠可以用于转义分隔符、引号字符或添加特殊字符(如\\n)。- schema
描述表的 Schema。如果提供,它将用于满足
col_names和col_types。- col_names
如果为
TRUE,则输入的第一行将用作列名,并且不包含在数据框中。如果为FALSE,则列名将由 Arrow 生成,从 "f0"、"f1"、...、"fN" 开始。或者,您可以指定一个字符向量作为列名。- col_types
列类型的紧凑字符串表示、Arrow Schema 或
NULL(默认值)以从数据中推断类型。- col_select
要保留的列名的字符向量,类似于
data.table::fread()中的 "select" 参数,或 tidy selection 规范的列,如dplyr::select()中使用。- na
要解释为缺失值的字符串字符向量。
- quoted_na
引号内的缺失值应视为缺失值(默认)还是字符串。(请注意,这与 Arrow C++ 默认的相应转换选项
strings_can_be_null不同。)- skip_empty_rows
是否完全忽略空白行?如果为
TRUE,则空白行将完全不表示。如果为FALSE,则将用缺失值填充它们。- skip
读取数据前要跳过的行数。
- parse_options
请参阅 CSV 解析选项。如果给定,这将覆盖在其他参数(例如
delim、quote等)中提供的任何解析选项。- convert_options
请参阅 CSV 转换选项
- read_options
请参阅 CSV 读取选项
- as_data_frame
函数应该返回一个
tibble(默认)还是一个 Arrow Table?- timestamp_parsers
用户定义的时间戳解析器。如果指定了多个解析器,CSV 转换逻辑将尝试从该向量的开头开始解析值。可能的值为
NULL:默认值,使用 ISO-8601 解析器一个字符向量的 strptime 解析字符串
一个 TimestampParser 对象列表
- decimal_point
浮点数中用于十进制点的字符。
详情
read_csv_arrow() 和 read_tsv_arrow() 是 read_delim_arrow() 的包装函数,它们指定了分隔符。 read_csv2_arrow() 使用 ; 作为分隔符,, 作为小数点。
请注意,并非所有 readr 选项都已在此处实现。如果您遇到 arrow 应该支持的选项,请提出问题。
如果您需要控制 readr::read_csv() 中没有等效的 Arrow 特定读取器参数,您可以在 parse_options、convert_options 或 read_options 参数中提供它们,或者您可以直接使用 CsvTableReader 进行低级访问。
指定列类型和名称
默认情况下,CSV 读取器将从文件中推断列名和数据类型,但有几种方法可以直接指定它们。
一种方法是在 schema 参数中提供 Arrow Schema,它是一个从列名到类型的有序映射。当提供时,它同时满足 col_names 和 col_types 参数。如果您预先知道所有这些信息,这会很有用。
您还可以将 Schema 传递给 col_types 参数。如果这样做,列名仍将从文件中推断,除非您也指定 col_names。在任何一种情况下,Schema 中的列名必须与数据的列名匹配,无论是明确提供还是推断。也就是说,此 Schema 不必引用所有列:省略的列将推断其类型。
或者,您可以通过向 col_types 参数提供 readr 使用的紧凑字符串表示来声明列类型。这意味着您提供一个单个字符串,每个列一个字符,其中字符映射到 Arrow 类型,类似于 readr 类型映射
"c":
utf8()"i":
int32()"n":
float64()"d":
float64()"l":
bool()"f":
dictionary()"D":
date32()"t":
time32()(unit参数设置为默认值"ms")"_":
null()"-":
null()"?": 从数据中推断类型
如果您对 col_types 使用紧凑字符串表示,则还必须指定 col_names。
无论如何指定类型,所有类型为 null() 的列都将被删除。
请注意,如果您正在指定列名,无论是通过 schema 还是 col_names,并且 CSV 文件包含一个原本用于标识列名的标题行,您需要添加 skip = 1 来跳过该行。
示例
tf <- tempfile()
on.exit(unlink(tf))
write.csv(mtcars, file = tf)
df <- read_csv_arrow(tf)
dim(df)
#> [1] 32 12
# Can select columns
df <- read_csv_arrow(tf, col_select = starts_with("d"))
# Specifying column types and names
write.csv(data.frame(x = c(1, 3), y = c(2, 4)), file = tf, row.names = FALSE)
read_csv_arrow(tf, schema = schema(x = int32(), y = utf8()), skip = 1)
#> # A tibble: 2 x 2
#> x y
#> <int> <chr>
#> 1 1 2
#> 2 3 4
read_csv_arrow(tf, col_types = schema(y = utf8()))
#> # A tibble: 2 x 2
#> x y
#> <int> <chr>
#> 1 1 2
#> 2 3 4
read_csv_arrow(tf, col_types = "ic", col_names = c("x", "y"), skip = 1)
#> # A tibble: 2 x 2
#> x y
#> <int> <chr>
#> 1 1 2
#> 2 3 4
# Note that if a timestamp column contains time zones,
# the string "T" `col_types` specification won't work.
# To parse timestamps with time zones, provide a [Schema] to `col_types`
# and specify the time zone in the type object:
tf <- tempfile()
write.csv(data.frame(x = "1970-01-01T12:00:00+12:00"), file = tf, row.names = FALSE)
read_csv_arrow(
tf,
col_types = schema(x = timestamp(unit = "us", timezone = "UTC"))
)
#> # A tibble: 1 x 1
#> x
#> <dttm>
#> 1 1970-01-01 00:00:00
# Read directly from strings with `I()`
read_csv_arrow(I("x,y\n1,2\n3,4"))
#> # A tibble: 2 x 2
#> x y
#> <int> <int>
#> 1 1 2
#> 2 3 4
read_delim_arrow(I(c("x y", "1 2", "3 4")), delim = " ")
#> # A tibble: 2 x 2
#> x y
#> <int> <int>
#> 1 1 2
#> 2 3 4