【问题标题】:Is it possible to keep memory while using apply()?使用 apply() 时是否可以保留内存?
【发布时间】:2020-07-10 06:13:50
【问题描述】:

我需要在activation_status 列表t 上运行函数lapply,以便函数的t 迭代记住t-1 迭代的结果。

该列表基本上是一个二维数组,表示多个 t 期间的单个项目 i 状态,如下所示:

n_items <<- 100
n_iterations <<- 10

activation_status <- 
  lapply(1:n_iterations, 
         FUN = function(t, bool, i) rep(bool, t), 
         FALSE, n_items)

现在在每次迭代 t 期间,我随机激活(设置为 TRUE)列表中的一些项目,但我希望在时间 t-1 已激活的所有项目保持活动状态(请注意,我在 update 函数中定义 activation_status 以便它可以在内部函数中访问)。

updateActivation <- function(t) {
  activation_status[[t]] <- as.logical(rbinom(n_items, 1, prob = .5))
  activation_status[[t]][activation_status[[t-1]] == TRUE] <- TRUE
}

然后

lapply(1:n_iterations, updateActivation)

抛出错误:

activation_status[[t - 1]] 中的错误:尝试在 get1index 中选择少于一个元素

我知道我可以使用循环,但我想知道是不是这样:

  1. 可以用apply 函数做这样的事情吗?
  2. 做得更快?

【问题讨论】:

    标签: r scope iteration lapply


    【解决方案1】:

    不确定我是否完全理解了这个问题,但似乎您正在寻找递归。
    在这种情况下,可以使用Reduce() 代替lapply()

    activation_status <- rep(FALSE, 10)
    n_iterations      <- 5
    Reduce(function(y, x) as.logical(rbinom(length(y), 1, prob=0.1)) | y,
           x=1:n_iterations, init=activation_status, accumulate=TRUE
           )
    
    [[1]]                                                           
     [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
    
    [[2]]                                                           
     [1] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE
    
    [[3]]                                                           
     [1] FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE
    
    [[4]]                                                           
     [1] FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE
    
    [[5]]                                                           
     [1]  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE
    
    [[6]]                                                           
     [1]  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE
    

    【讨论】:

      【解决方案2】:

      我们或许可以在不使用任何apply 命令的情况下做到这一点。

      #Set seed for reproduciblity
      set.seed(123)
      #Create initialization demo data
      activation_status <- rep(FALSE, 10)
      #Number of values to select
      n_iterations <- 5
      #Sequence from 1:n_iterations
      seq_n_iterations <- seq_len(n_iterations)
      #Create matrix to hold output
      output <- replicate(n_iterations, activation_status)
      #Select n_iterations random values from 1:length(activation_status)
      #You can change this if you want to use some specific distrubution
      points <- sample(length(activation_status), n_iterations)
      #Create column indices
      cols <- rep(seq_n_iterations, seq_n_iterations)
      #Create row indices
      rows <- points[ave(inds, inds, FUN = seq_along)]
      #Change those values to TRUE
      output[cbind(rows, cols)] <- TRUE
      
      output
      #       [,1]  [,2]  [,3]  [,4]  [,5]
      # [1,] FALSE FALSE FALSE FALSE FALSE
      # [2,] FALSE FALSE  TRUE  TRUE  TRUE
      # [3,]  TRUE  TRUE  TRUE  TRUE  TRUE
      # [4,] FALSE FALSE FALSE FALSE FALSE
      # [5,] FALSE FALSE FALSE FALSE FALSE
      # [6,] FALSE FALSE FALSE FALSE  TRUE
      # [7,] FALSE FALSE FALSE FALSE FALSE
      # [8,] FALSE FALSE FALSE  TRUE  TRUE
      # [9,] FALSE FALSE FALSE FALSE FALSE
      #[10,] FALSE  TRUE  TRUE  TRUE  TRUE
      

      如果您希望它们作为列表:

      asplit(output, 2)
      
      #[[1]]
      # [1] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
      
      #[[2]]
      # [1] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
      
      #[[3]]
      # [1] FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
      
      #[[4]]
      # [1] FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE
      
      #[[5]]
      # [1] FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE  TRUE
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-15
        • 2017-09-19
        • 2015-04-01
        • 2012-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多