I have a data set that contains multiple attributes with integer values from 1 to 5 and I would like to rescale these attributes so that their values range from -1 to 1. My current code that I have is
newdata$Rats = rescale(newdata$Rats, to = c(-1,1), from=c(1,5))
Where newdata
is my dataset and Rats
is one of my attributes. If I only had a few attributes to change that would be fine, but I have about 30 or so to change. Is there a way to use a for loop to do this or use the select
function that R has or possibly another way?
Use lapply()
:
newdata[, c(1:30)] <- lapply(newdata[, c(1:30)],
function(x) rescale(x, to = c(-1, 1), from = c(1, 5)))
For the c(1:30)
, insert a vector of either positions of your variables within your dataframe, or a vector of the names of your variables as strings.