Repeat matrix n-times into a list

后端 未结 1 1470
挽巷
挽巷 2021-01-18 21:17

I have a matrix that i want to duplicate n times in a list. Obviously the rep() function does not work on matrices, so does anyone have a good suggestion how to make this be

1条回答
  •  别那么骄傲
    2021-01-18 21:51

    You can use either lapply()

    n <- 3
    
    x <- lapply(seq_len(n), function(X) jwprox)
    str(x)
    # List of 3
    #  $ : logi [1:15, 1:15] NA NA NA NA NA NA ...
    #  $ : logi [1:15, 1:15] NA NA NA NA NA NA ...
    #  $ : logi [1:15, 1:15] NA NA NA NA NA NA ...
    

    or replicate():

    xx <- replicate(n, jwprox, simplify=FALSE)
    identical(x,xx)
    # [1] TRUE
    

    (FWIW, replicate() is just a sometimes-handy wrapper for sapply() and, in turn, lapply().)

    0 讨论(0)
提交回复
热议问题