3

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!

1
  • 1
    lapply returns lists by default. To return a dataframe, we can keep the original data structure with empty brackets and assign the output of the function to it's values: DF2[] <- lapply(DF1, ... Commented Sep 16, 2022 at 19:54

1 Answer 1

5

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"
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! This worked very well. Out of curiosity, from searching I know that |> is the pipe operator. How does that work with that syntax you have as an argument in lapply()?
glad I could help. This synthax is short for the lambda fuction. And the pipe sends the object in the left hand side as the first argument to the function in the right hand side. y |> lapply(\(x) some_function(x)) is the same as lapply(y, function(x) some_function(x))
This blog post about pipes is very clear and informative: ivelasq.rbind.io/blog/understanding-the-r-pipe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.