R: Confusion with apply() vs for loop

后端 未结 2 1686
轻奢々
轻奢々 2020-12-30 17:35

I know that I should avoid for-loops, but I\'m not exactly sure how to do what I want to do with an apply function.

Here is a slightly simplified model of what I\'

2条回答
  •  旧巷少年郎
    2020-12-30 17:59

    Using an apply function to do your regression is mostly a matter of preference in this case; it can handle some of the bookkeeping for you (and so possibly prevent errors) but won't speed up the code.

    I would suggest using vectorized functions though to compute your first's and last's, though, perhaps something like:

    window <- 5
    ng <- 15 #or ncol(g)
    xy <- data.frame(first = pmax( (1:ng) - window, 1 ), 
                      last = pmin( (1:ng) + window, ng) )
    

    Or be even smarter with

    xy <- data.frame(first= c(rep(1, window), 1:(ng-window) ), 
                     last = c((window+1):ng, rep(ng, window)) )
    

    Then you could use this in a for loop like this:

    results <- list()
    for(i in 1:nrow(xy)) {
      results[[i]] <- xy$first[i] : xy$last[i]
    }
    results
    

    or with lapply like this:

    results <- lapply(1:nrow(xy), function(i) {
      xy$first[i] : xy$last[i]
    })
    

    where in both cases I just return the sequence between first and list; you would substitute with your actual regression code.

提交回复
热议问题