Loop in R: how to save the outputs?

好久不见. 提交于 2019-11-28 11:20:57

Store each boolean matrix as an item in a list:

result <- vector("list",49)
for (i in 1:49)
{
   result[[i]] <- T1>F[i] # I used print to see the results in the screen
}

#Print the first matrix on screen
result[[1]]

Another way to do what joran suggests is to use the apply family of functions.

result2 <- lapply(F, function(f) {T1 > f})

This gives the same thing as joran's result, a list where each element corresponds to one of the values of F and that element is a 26x26 logical matrix.

Another alternative is to store the results as a three dimensional logical matrix (49*26*26) where each slice corresponds to one of the values of F.

result3 <- sapply(F, function(f) {T1 > f}, simplify="array")

the structure of which is

> str(result3)
 logi [1:26, 1:26, 1:49] FALSE FALSE FALSE FALSE TRUE TRUE ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!