I have some .csv files which I would like to open, specifying the default column type as "i" for integer. However, certain files also have specific column which I would like to tell readr::read_csv
to open with defined types (the logic of which columns doesn't matter, let's assume I know which ones for which files)
Is there a way to pass these columns into the col_types
argument of read_csv
while still maintaining that every other column should be opened with integer type
df <- data.frame(
a = c(1,2,3,4),
b = sample(1:100, 4),
c_text = c("hi", "I", "am", "text"),
d_decimals = runif(4),
e_more_text = c("another", "text", "column", "lol")
)
readr::write_csv(df, "/path/to/csv/file.csv")
character_cols <- c("c_text", "e_more_text")
double_cols <- "d_decimals"
data <- readr::read_csv(
"/path/to/csv/file.csv",
# supply something here to determine column types
col_types = cols(.default = "i", character_cols = "c", double_cols = "d")
)
because of the logic in calculating which columns should be characters or doubles, etc. I'd ideally supply them as a vector of names
Cheers