tidyverse / readr

Read flat files (csv, tsv, fwf) into R
https://readr.tidyverse.org
Other
1.01k stars 286 forks source link

`parse_number()` option to ignore numeric columns #1543

Open jakub-jedrusiak opened 4 months ago

jakub-jedrusiak commented 4 months ago

I often have to bulk convert a set of columns, some of which are of character type, while the others are double. When I try to use the parse_number() function I get:

Caused by error in `parse_vector()`:
! variable is.character(x) is not TRUE

It would be really handy if I could set something like ignore_numbers = TRUE for the function to just return the numeric column unchanged.

A simple use case would be if I wanted to convert multiple columns with analogous names without bothering about some of them already being numeric:

library(tidyverse)

df <- tibble(column_1 = 1:5, column_2 = paste0(1:5, letters[1:5]))

df %>%
  mutate(across(starts_with("column_"), parse_number))

A workaround would be:

library(tidyverse)

df <- tibble(column_1 = 1:5, column_2 = paste0(1:5, letters[1:5]))

df %>%
  mutate(across(starts_with("column_"), \(x) if (is.character(x)) parse_number(x) else x)
chainsawriot commented 3 months ago

@jakub-jedrusiak Sounds like a use case of mutate_if.