Home > Software design >  Using `scale()` to normalize all numeric columns in a data.frame
Using `scale()` to normalize all numeric columns in a data.frame

Time:07-30

I wish to normalize my entire data.frame. Is it possible to do so? Of course, I want to keep the dates as they are.

dat <- structure(list(date = c("2021-01-01", "2021-01-02", "2021-01-03", 
"2021-01-04", "2021-01-05", "2021-01-06"), launches = c(4, 0, 
0, 0, 8, 4), pledged = c(50278.64, 0, 0, 0, 366279.590415302, 
172073.0471292), backers = c(2880, 0, 0, 0, 6588, 3528), total_goal = c(24000, 
0, 0, 0, 148000, 60000), mean_goal = c(6000, 0, 0, 0, 18500, 
15000), US = c(4, 0, 0, 0, 4, 0), `number of success` = c(4, 
0, 0, 0, 8, 4), duration_days = c(30, 0, 0, 0, 31, 30), Twitter = c(1324L, 
1548L, 1297L, 1585L, 1636L, 1583L), replies = c(882L, 1252L, 
910L, 1018L, 810L, 1000L), likes = c(22859L, 24375L, 17854L, 
20341L, 19521L, 19401L), retweets = c(8621L, 8239L, 6141L, 6728L, 
6938L, 6842L), group_date = c("01", "01", "01", "01", "01", "01"
)), row.names = c(NA, 6L), class = "data.frame")

CodePudding user response:

We can do

j <- sapply(dat, is.numeric)
dat[j] <- scale(dat[j])
  • Related