I am trying to read a google sheet with many columns (gs1
) into R. I also have a separate data frame in R (df_col
) that has a list of the columns in gs1
along with their formats. Is there a way to read gs1
into R using the columns formats specified in df_col
?
For example:
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
To read in gs1
:
gs1_df <- read_sheet(ss = "gs1", sheet = "Sheet1", col_types = df_col$col_type)
Whenever I currently follow the logic in the example above, I get an error stating Error in check_length_one():! col_types must have length 1, not length 3.
I'm guessing this is because there is an issue with directly calling the column of df_col
in the function but I'm not sure how to get around that. Does the column need to be converted to a string before using it in the function?
CodePudding user response:
The documentation sets out that col_types
is expected to be a
string of readr-style shortcodes, with one character or code per column
You have currently got the short codes correctly defined, but in a vector of length nrow(df_col)
. You need to convert it to a string (or technically a character vector of length one).
col_types <- paste(df_col$col_type, collapse = "")
gs1_df <- read_sheet(ss = "gs1", sheet = "Sheet1", col_types = col_types)