Non-numeric values in columns

Hi,
How should I check if I have for example a letters or combination of letters and numbers or spaces in a column that type is a "character" ? I would like to know in which rows do I have it in my dataframe ? I would like to make a subset of it and look at it before I convert character column to numeric one and then values like "BBB" or "12ba" will become NA. I want to have only values like " 123", 321" in a particular column.

Any ideas will be much appreciated,
Thanks

suppressPackageStartupMessages({
  library(dplyr)
  library(stringr)
})

lookfor <- "^\\d+$"

entry <- "1#a"
reentry <- "123"

make_all_numeric <- function(x) ifelse(str_detect(x,lookfor),x,NA)

make_all_numeric(entry)
#> [1] NA
make_all_numeric(reentry)
#> [1] "123"

my_df <- data.frame(VAR = c(entry,reentry))

my_df %>% mutate(VAR = make_all_numeric(VAR))
#>    VAR
#> 1 <NA>
#> 2  123

Created on 2020-12-09 by the reprex package (v0.3.0.9001)

Thank you technocrat, that was very helpful.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.