Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An Elegant way to change columns type in dataframe in R

Tags:

r

I have a data.frame which contains columns of different types, such as integer, character, numeric, and factor.

I need to convert the integer columns to numeric for use in the next step of analysis.

Example: test.data includes 4 columns (though there are thousands in my real data set): age, gender, work.years, and name; age and work.years are integer, gender is factor, and name is character. What I need to do is change age and work.years into a numeric type. And I wrote one piece of code to do this.

test.data[sapply(test.data, is.integer)] <-lapply(test.data[sapply(test.data, is.integer)], as.numeric)

It looks not good enough though it works. So I am wondering if there is some more elegant methods to fulfill this function. Any creative method will be appreciated.

like image 897
wanglin Avatar asked May 19 '16 06:05

wanglin


People also ask

How do I change the type of column in a Dataframe in R?

You can change data types using as. * where * is the datatype to change to, the other way is using class(). class(df$var) = "Numeric".

How do I change datatype of all columns in R?

To convert columns of an R data frame from integer to numeric we can use lapply function. For example, if we have a data frame df that contains all integer columns then we can use the code lapply(df,as. numeric) to convert all of the columns data type into numeric data type.

How do I set Colnames in R?

Method 1: using colnames() method colnames() method in R is used to rename and replace the column names of the data frame in R. The columns of the data frame can be renamed by specifying the new column names as a vector. The new name replaces the corresponding old name of the column in the data frame.


1 Answers

I think elegant code is sometimes subjective. For me, this is elegant but it may be less efficient compared to the OP's code. However, as the question is about elegant code, this can be used.

test.data[] <- lapply(test.data, function(x) if(is.integer(x)) as.numeric(x) else x)

Also, another elegant option is dplyr

library(dplyr)
library(magrittr)
test.data %<>% 
      mutate_each(funs(if(is.integer(.)) as.numeric(.) else .))
like image 189
akrun Avatar answered Oct 18 '22 05:10

akrun