Search code examples
rfunctionscaledata-manipulation

Disappearing Columns (R)


I am working with the R programming language. I am trying to use the "scale" function on some data I created:

a =rnorm(100,10,10)
b = rnorm(100,10,5)
c <- sample( LETTERS[1:4], 100 , replace=TRUE, prob=c(0.25, 0.22, 0.25, 0.25) )

d = data.frame(a,b,c)
d$c = as.factor(d$c)

I only want to use the "scale" function on the first two columns in this data set:

e = scale(d[,1:2])

However, this returns a data set with only two columns. I want to use the "scale" function on the first two columns, and keep the last column as is. That is, the final data set "e" should have 3 columns.

Is there a way to only apply the "scale" function on the first two columns?

Thanks

NOTE:

Of course, I could this manually:

d$a = scale(d$a)
d$b = scale(d$b)

But I am looking for a way to apply the "scale" function on multiple columns at the same time. Does anyone know how to do this?


Solution

  • You could replace the columns.

    cols <- 1:2
    d[cols] <- scale(d[cols])