It would help in the future if you could provide a reproducible example, with a version of train
.
So for example building an example data.frame train
:
train <- data.frame(X1 = 1:50,
X2 = 50:1,
X3 = 201:250,
X4 = 250:201,
X5 = 251:300)
for(i in 1:5){
v <- paste("X",i,sep="")
newdata <- subset(train, abs(scale(train[[v]])) >= 3)
cat("There are", nrow(newdata) ,"potential v outliers >= |3| in train","\n")
}
#> There are 0 potential v outliers >= |3| in train
#> There are 0 potential v outliers >= |3| in train
#> There are 0 potential v outliers >= |3| in train
#> There are 0 potential v outliers >= |3| in train
#> There are 0 potential v outliers >= |3| in train
Created on 2022-12-12 by the reprex package (v2.0.1)
There are two problems in your code, first:
here, you have "i"
in quotes, so R understands the character string "i"
. You mean the value of the variable i
, so you should not use quotes:
v <- paste("X",i,sep="")
Second, after this command, v
is a variable that contains the character string "X1"
(for i=1
). But what you want to scale is not "X1", it's the column of train
that is named X1
. So you can call it with train[[v]]
, which means "select the column of train
which is named as the value of the variable v
".