pass text as specified columns to open as [type] using read_csv from readr in R

Viewed 170

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

1 Answers

You can make a helper function with combines your extra spec with the default column spec, then pulling the spec together with do.call.

extra_spec = list(
  "c_text" = "c",
  "d_decimals" = "i",
  "e_more_text" = "c"
)

read_csv_with_default_int = function(path, extra_spec) {
  readr::read_csv(path, col_types = do.call(cols, c(extra_spec, list(.default = col_integer()))))
}

read_csv_with_default_int("file.csv", extra_spec = extra_spec)

You could also avoid the lots of nested logic with a helper like

cols_default_int = purrr::partial(cols, .default = col_integer())

read_csv_with_default_int = function(path, col_types) {
  readr::read_csv(path, col_types = do.call(cols_default_int, col_types))
}

read_csv_with_default_int("file.csv", col_types = extra_spec)
Related