Home > OS >  How do you convert all numeric strings in a dataframe to numeric in R?
How do you convert all numeric strings in a dataframe to numeric in R?

Time:09-18

I'm pretty new to R and I have a large dataframe that I have read from a file. The data inside has columns with data that are both "normal" strings like "Iceland" but it also has columns that have numerics but as strings, like "25.1". Is there a way to convert all strings that are of only numbers to numerics?

I know how to do it for one column: var <- as.numeric(dataFrame$var) but this isn't very effective as my data frame has about 160 columns.

I've tried to convert the entire dataframe as follows, but it doesn't work:

DF2 <- lapply(DF1, function(x) {
  ifelse(!is.na(suppressWarnings(as.numeric(x))), as.numeric(as.character(x)), x)
})

Testing a column with DF2$colName returns "character", not "numeric". This also seems to turn the dataframe into a list.

As a side note, I've read in the dataframe using read.csv() without any arguments except for the filename. Additionally, the columns don't mix types, it's either all normal strings or all numeric strings.

Thank you!

CodePudding user response:

we can use the type.convertfunction

example data

df <- data.frame(a=c("2.1"), b='iceland')

check classes in original example data:

df |> lapply(\(x) class(x))

$a
[1] "character"

$b
[1] "character"

Transformation

df <- type.convert(df, as.is =TRUE)

testing output

df |> lapply(\(x) class(x))

$a
[1] "numeric"

$b
[1] "character"
  • Related