thehand0
thehand0

Reputation: 1163

Rescaling multiple columns of dataframe between specific ranges in R

I'm wanting to rescale multiple columns of a dataframe between a specific range (1 and 20) in R/R Studio. While I can get it to work for a single column, I cant seem tog et it wo work for multiple. The real data contains many columns, so some sort of indexing would be ideal if possible. I'm sure it's probably something simple, but cant seem to figure out what I'm missing. Any help would be appreciated. Thanks

# This works on a single column
library(scales)
single = c(100,90,80,70,60,50,40,30,20,10)
rescale(single , to=c(1,20))

# This does not work
library(scales)
multiple = data.frame(V0 = c("A","B","C","D","E","F", "G", "H", "I", "J"),
                     V1= c(1,2,3,4,5,6,7,8,9,10),
                     V2= c(100,90,80,70,60,50,40,30,20,10)
                     )
                
rescale(multiple[,c(2,3)], to=c(1,20))

Upvotes: 0

Views: 234

Answers (1)

Onyambu
Onyambu

Reputation: 79208

You are looking for:

multiple[,c(2,3)] <-lapply(multiple[,c(2,3)], rescale, to=c(1,20))

Upvotes: 1

Related Questions