-4

I have a large number of matrix, calls train which is a binary data with 1 and 0

I want to extract and make another two lists which contains 1 as list1 and 0 as list2 by using for loop

my R code is not working

X <- c(0,1,0,1,0,1)
Y <- c(1,1,1,1,1,0)
train<- as.matrix (cbind(X,Y))
list1 <- list()
list2 <- list()

for(i in 1:length(train)) {
 if(train[i]== 1)
    list1 = train[i]
 else
    list2 = train[i]

}

Therefore I want my list1 to contain (1,1,1,1,1,1,1) and list2 to contain (0,0,0,0)

Hung
  • 1
  • 4

1 Answers1

0

Perhaps you don't need a for loop, just select your data using a logical evaluation. By the way, the data structure you want in R is called vector, not list. Please refer to this website (http://www.programcreek.com/2014/01/vector-array-list-and-data-frame-in-r/) for more information.

list1 <- train[train == 1]
list2 <- train[train == 0]
www
  • 38,575
  • 12
  • 48
  • 84