Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scale only certain columns R [closed]

Tags:

dataframe

r

How can I scale(x) only certain columns of a dataframe? I have a dataframe with 7 columns and I want to scale only column 3 and 6. The rest should stay as it is.

like image 627
RenaSo Avatar asked Apr 19 '18 15:04

RenaSo


People also ask

Why do we need column scaling in R?

This type of analysis often needs column scaling in a data frame to provide meaningful results. Without normalizing, the vectors or columns you are using, you will usually get meaningless results. The scale () is a built-in R function whose default method centers and/or scales the columns of a numeric matrix.

How to standardize data using the scale function in R?

The following R syntax shows how to standardize our example data using the scale function in R. As you can see in the following R code, we simply have to insert the name of our data frame (i.e. data) into the scale function: The previous output of the RStudio console shows the first six rows of our standardized data.

How many columns are in the example data in R?

It shows that our example data consists of two numeric columns x1 and x2. The following R syntax shows how to standardize our example data using the scale function in R.

How to select some columns of a data frame in R?

The most common way to select some columns of a data frame is the specification of a character vector containing the names of the columns to extract. Consider the following R code: data [ , c ("x1", "x3")] # Subset by name. data [ , c ("x1", "x3")] # Subset by name.


1 Answers

We can do this with lapply. Subset the columns of interest, loop through them with lapply, assign the output back to the subset of data. Here, we are using c because the outpuf of scale is a matrix with a single column. Using c or as.vector, it gets converted to vector

df[c(3,6)] <- lapply(df[c(3, 6), function(x) c(scale(x)))

Or another option is mutate_at from dplyr

library(dplyr)
df %>%
   mutate_at(c(3,6), funs(c(scale(.))))
like image 157
akrun Avatar answered Nov 14 '22 22:11

akrun