Loop in R: how to save the outputs?

前端 未结 2 1341
[愿得一人]
[愿得一人] 2020-12-09 22:01

I am trying to save the data from a loop of logical tests.

So I have the following data:

T1 <- matrix(seq(from=100000, to=6600000,length.out=676),         


        
相关标签:
2条回答
  • 2020-12-09 22:33

    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 ...
    
    0 讨论(0)
  • 2020-12-09 22:46

    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]]
    
    0 讨论(0)
提交回复
热议问题