Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - Scaling numeric values only in a dataframe with mixed types

Tags:

r

I am working with a data frame that has mixed data types (numeric and character) and also has a character key as the primary identifier. I'd like to scale and center the numeric variables. I've tried using the scale() function, but it requires all fields to be numeric. When I take just the numeric fields and scale them, I have to drop the character identifier to be able to scale them.

My ideal end state is that I have a data frame with character fields and scaled numeric fields.

I realize this is a newbie question, so please be gentle ;-)

Thanks!

Jim

like image 675
Jim Murphy Avatar asked May 12 '14 21:05

Jim Murphy


People also ask

How do I scale a numeric variable in R?

In R, you can use the scale() function to scale the values in a vector, matrix, or data frame. You will almost always receive meaningless results if you do not normalize the vectors or columns you are utilizing. Scale() is a built-in R function that centers and/or scales the columns of a numeric matrix by default.

What does R scale function do?

scale() function in R Language is a generic function which centers and scales the columns of a numeric matrix. The center parameter takes either numeric alike vector or logical value. If the numeric vector is provided, then each column of the matrix has the corresponding value from center subtracted from it.

How do I convert a dataset to numeric in R?

Data Visualization using R Programming 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.


2 Answers

Something like this should do what you want:

library(MASS)
ind <- sapply(anorexia, is.numeric)
anorexia[ind] <- lapply(anorexia[ind], scale)
like image 104
James King Avatar answered Oct 19 '22 05:10

James King


This can be done straightforwardly using dplyr::mutate_if:

library(dplyr)

iris %>%
    mutate_if(is.numeric, scale)
like image 44
Marius Avatar answered Oct 19 '22 04:10

Marius