[英]R extract data by for loop
I have a large number of matrix, calls train which is a binary data with 1 and 0我有大量矩阵,调用 train 是一个二进制数据,带有 1 和 0
I want to extract and make another two lists which contains 1 as list1 and 0 as list2 by using for loop我想通过使用 for 循环提取并制作另外两个列表,其中包含 1 作为 list1 和 0 作为 list2
my R code is not working我的 R 代码不起作用
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)因此我希望我的 list1 包含 (1,1,1,1,1,1,1) 和 list2 包含 (0,0,0,0)
Perhaps you don't need a for loop, just select your data using a logical evaluation.也许您不需要 for 循环,只需使用逻辑评估选择您的数据。 By the way, the data structure you want in R is called
vector
, not list
.顺便说一下,你想要在 R 中的数据结构叫做
vector
,而不是list
。 Please refer to this website ( http://www.programcreek.com/2014/01/vector-array-list-and-data-frame-in-r/ ) for more information.有关更多信息,请参阅此网站 ( http://www.programcreek.com/2014/01/vector-array-list-and-data-frame-in-r/ )。
list1 <- train[train == 1]
list2 <- train[train == 0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.