I don't think the best way is a loop, it isn't a list that you want but a vector object, I propose to use ==
on a matrix as following :
X <- c(0,1,0,1,0,1)
Y <- c(1,1,1,1,1,0)
train <- cbind(X,Y)
v1 <- train[train == 1]
v2 <- train[train == 0]
If you really want a loop :
v1 <- c() # It is not necessary
v2 <- c() # It is not necessary
for(i in 1:nrow(train)){
for(j in 1:ncol(train)){
if(train[i, j] == 1){
v1 <- c(v1, train[i, j])
}else{
v2 <- c(v2, train[i, j])
}
}
}
A last solution but not least :
v1 <- rep(1, sum(train == 1))
v2 <- rep(0, sum(train == 0))
So their is a lot of differents solutions to do it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…