4

I'm using R to pull out numbers from strings of ids. In the past, I've used readr's parse_number() function, but recently, I'm getting a bizarre error where it's saying that my character column is not character:

library(dplyr)
library(readr)

test <- tibble(id_numbers = c("ICKLSD5", "DFTEM4", "DPDFE45"))

test %>%
  parse_number(id_numbers)


Error in parse_vector(x, col_number(), na = na, locale = locale, trim_ws = trim_ws) : 
  is.character(x) is not TRUE

But, the column is clearly a character vector:


#Yields true
is.character(test$id_numbers)

And, even wrapping the column in as.character still gives the same error:

test %>%
  parse_number(as.character(id_numbers))

I know there's other workarounds with other functions, but I really want to use parse_number(). Can anyone explain why I'm getting this error and how I can fix it?

J.Sabree
  • 2,280
  • 19
  • 48
  • 2
    `%>%` will pass your tibble as the first argument to `parse_number()`. Does `parse_number(test$id_numbers)` work? Or do you want a new column, and need a `mutate(newcol = parse_number(id_numbers))`? – Hobo Feb 05 '22 at 22:53
  • thank you both! I completely spaced on not having mutate in my call. – J.Sabree Feb 05 '22 at 22:55
  • 1
    @MrFlick I believe you should post as an answer. The comment is short but potentially useful to others. There are 2 close as typo votes but it's not a typo, it's a misinterpretation of the function's usage. – Rui Barradas Feb 05 '22 at 23:14

1 Answers1

6

You can't pipe your data frame directly into parse_number. You would need to pipe into a mutate:

test %>% mutate(id_numbers=parse_number(id_numbers))

assuming you want to change the data.frame itself. Because

test %>% parse_number(id_numbers)

is the same as

parse_number(test, id_numbers)

and test is not a character vector, it is a data.frame, hence you get the error.

MrFlick
  • 195,160
  • 17
  • 277
  • 295