2021年3月22日星期一

loop over rows of a data.frame and use them as input for a function

The input values (a and b) for my foo() function come from a expand.grid() call.

I was wondering how to loop over the rows of input data.frame such that at each round, one a and b from one row of input data.frame is used in foo() as a named list?

foo <- function(a, b) {     b-a    }    input <- rev(expand.grid(b=1:4,a=1:4))    #   a b  #1  1 1  # at round one, `a = 1`, `b = 1`  #2  1 2  #3  1 3  #4  1 4  # at round four, `a = 1`, `b = 4`  #.  . .  #.  . .  #.  . .    foo(a=, b=)    # Desired output:    [[1]] `a=1, b=1`        [1]  0    [[2]] `a=1, b=2`       [1]   1  # .  # .  # .  
https://stackoverflow.com/questions/66757402/loop-over-rows-of-a-data-frame-and-use-them-as-input-for-a-function March 23, 2021 at 12:45PM

没有评论:

发表评论