Home > database >  Why is the parse_number function saying my character vector is not a character?
Why is the parse_number function saying my character vector is not a character?

Time:02-06

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?

CodePudding user response:

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.

  •  Tags:  
  • Related