Question

I need to standardize all except one column in a dataframe, with which I'm using knn. I know that I can do this with loops, but it seems like there might be an easier way, especially since I am working with 200+ columns/factors, which would have to be renamed.

Any suggestions?

Was it helpful?

Solution

yes. I assumed you would prefer to identify the variable not to be scaled based on its name rather than identfying it by it's column position. Without a toy example I can only surmise this is exactly what you want.

x <- data.frame(replicate(10, rnorm(10)))
names(x) <- letters[1:10]

##let's say you don't want to scale "b"
scalevars <- setdiff(names(x), "b")
x.scaled <- data.frame(sapply(x[,scalevars], scale),b=x[,"b"])
x.scaled <- x.scaled[,names(x)] #to get the original order of variables
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top