What is the most efficient way to convert multiple columns in a data frame from character to numeric format?
I have a dataframe called DF with all character variables.
I would like to do something like
for (i in names(DF){
DF$i <- as.numeric(DF$i)
}
Thank you
I realize this is an old thread but wanted to post a solution similar to your request for a function (just ran into the similar issue myself trying to format an entire table to percentage labels).
Assume you have a df with 5 character columns you want to convert. First, I create a table containing the names of the columns I want to manipulate:
This is not ideal for large tables as it goes cell by cell, but it would get the job done.
You could try
this example from ARobertson was the most efficient I saw here. I used it to convert integers to numeric. Worked like I needed it to and no loops needed or long code.
solution
I think I figured it out. Here's what I did (perhaps not the most elegant solution - suggestions on how to imp[rove this are very much welcome)
If you're already using the tidyverse, this replaces all character columns with numeric, and leaves the rest alone:
You could use convert from the hablar package:
Which gives you:
Or if you are lazy, let retype() from hablar guess the right data type:
which gives you: