Built-in function scale will standardize each column of dataframe,but it will transformation original data frame into matrix.How to fix it?What if I just want to standardize only some columns?
Asked
Active
Viewed 317 times
-1
-
Can you use dput to provide some data and the code that you are running. – MatthewR May 10 '19 at 04:50
1 Answers
0
There's kind of two questions here. The first, how to insert into a data.frame
without losing the data.frame
structure. In this case use [<-
like so:
df <- data.frame(a=1:3, b=10:12, c=20:22)
df[] <- scale(df)
df
# a b c
#1 -1 -1 -1
#2 0 0 0
#3 1 1 1
This is covered in a lot more detail here: R: Easy assignments with empty square brackets? x[]<-
The second question is how to update a subset of a data.frame
. Again, use [<-
but this time match the selections on the left and right sides of the <-
:
df <- data.frame(a=1:3, b=10:12, c=20:22)
df[1:2] <- scale(df[1:2])
df
# a b c
#1 -1 -1 20
#2 0 0 21
#3 1 1 22

thelatemail
- 91,185
- 12
- 128
- 188