functional programming - How does the function argument work in R's 'combn'? -
functional programming - How does the function argument work in R's 'combn'? -
despite reading documentation, i'm struggling understand how function argument works in combn utility.
i have table 2 columns of data, each column, want calculate ratio of each unique combination of info pairs in column. let's focus on 1 column simplicity:
v1 1 342.3 2 123.5 3 472.0 4 678.3 ... 14 567.2 i can utilize next homecoming unique combinations:
combn(table[,1], 2) but of course of study returns each pair of values. want split them ratio, can't seem figure out how set up.
i understand outer, example, can provide operator argument how transfer combn?
combn(table[,1], 2, fun = "/") # not right
the issue function receive 1 parameter. , parameter vector of elements in particular set. / function require 2 separate parameters, not single vector of values. instead write
combn(table[,1], 2, fun = function(x) x[1]/x[2]) so here 1 parameter x , split first value second.
other functions such
combn(1:4, 2, fun = sum) work fine because expect receive single vector of values.
r functional-programming combinations
Comments
Post a Comment