我正在尝试将一个包含多列(gs1
(的google工作表读取到R中。我在R中也有一个单独的数据框架(df_col
(,其中包含gs1
中的列列表及其格式。有没有一种方法可以使用df_col
中指定的列格式将gs1
读取为R?
例如:gs1
col1 col2 col3
a 1 01/01/22
b 2 01/02/22
c 3 01/03/22
df_col
col_name col_type
col1 c
col2 i
col3 D
在gs1
:中读取
gs1_df <- read_sheet(ss = "gs1", sheet = "Sheet1", col_types = df_col$col_type)
每当我当前遵循上面例子中的逻辑时,我都会得到一个错误,说明Error in check_length_one():! col_types must have length 1, not length 3.
我猜这是因为在函数中直接调用df_col
列存在问题,但我不知道如何解决这个问题。在函数中使用该列之前,是否需要将其转换为字符串?
文档规定col_types
应为
readr风格的短代码字符串,每列一个字符或代码
您目前已经正确定义了短代码,但长度为nrow(df_col)
。您需要将其转换为字符串(或者从技术上讲是长度为1的字符向量(。
col_types <- paste(df_col$col_type, collapse = "")
gs1_df <- read_sheet(ss = "gs1", sheet = "Sheet1", col_types = col_types)