runningbirds runningbirds - 1 month ago 8
R Question

Check if each value in the column is a string or numeric

I would like to figure out whether or not each value in my column is a numerical value or a non-numerical value.

df = data.frame(help = c(456, 'superduper'),
correct_answer = c("numeric","string"))


I've tried is.numeric or is.character, on a row-by-row basis but this doesnt seem to work as the entire column is coded as a character.

I know this is a basic question, but any help is appreicated.

Answer

My first thought would be to consider if the value can be coerced to a number. E.g.:

is.na(as.numeric(as.character(df$help)))
#[1] FALSE  TRUE

The bonus is that this will work for variations on number formats like 3, 3.2 or even 3e5

Comments