Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse_number() option to ignore numeric columns #1543

Open
jakub-jedrusiak opened this issue May 31, 2024 · 1 comment
Open

parse_number() option to ignore numeric columns #1543

jakub-jedrusiak opened this issue May 31, 2024 · 1 comment

Comments

@jakub-jedrusiak
Copy link

jakub-jedrusiak commented May 31, 2024

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants