0

I'm using the built in dataset "ChickWeight". For each name, weight, Time, Chick, Diet, my function is simply taking the difference between each pair of names, weight Time, weight Chick, weight Diet, Time Chick, Time Diet, Chick Diet. The function and calculation itself are rather simple and unecesary, but I would like to find how I can pass the 2 parameter function in the combination.

My script is as follows

out <- combn(names(ChickWeight), 2, simplify=FALSE)

f <- function(x, y)
{
diff <- (x - y)
}

mapply(f, out[1,], out[2,])

UPDATE: It seems that I need to subtract the numeric values in my function f, NOT the names. I'm wondering how I may do this. Perhaps I need to find a way to reference out[1,] and out[2,] so that the numeric values in the respective columns can be called upon.

Frank
  • 66,179
  • 8
  • 96
  • 180
Luke Zhang
  • 343
  • 1
  • 4
  • 14
  • Have you checked `out[1,]`? The `f` is getting the difference between `names` and not on numeric elements – akrun Jun 14 '16 at 18:34
  • You should use simplify=TRUE in `comb`. Then your `mapply` function should work. – lmo Jun 14 '16 at 18:37
  • Then you'll get a different error since you are subtracting characters – Rich Scriven Jun 14 '16 at 18:38
  • Thanks guys. Is there a way to reference ChickWeight with out[1,] and out[2,] so that my function substracts the numbers within their respective columns? – Luke Zhang Jun 14 '16 at 20:33

1 Answers1

2

I would like to find how I can pass the 2 parameter function in the combination.

Ok, so there are two problems. The programming one is resolved by something like

combn(names(ChickWeight), 2, function(x) ChickWeight[[x[1]]] - ChickWeight[[x[2]]])

combn always takes a single-parameter function, confusingly.


The conceptual problem is: Chick is ordinal data and Diet is categorical. Neither of these make sense in arithmetic operations like subtraction (which is why the code above gives warnings and NAs).

Frank
  • 66,179
  • 8
  • 96
  • 180
  • Thanks a lot for this. I've tried using a different data set and it works fine. Having only one paramater may make things a bit more difficult as I eventually want to use a function that is more complicated. For eg. I would like to find the coefficient between the datasets via lm(). Would you recommend I use a command other than combn? – Luke Zhang Jun 14 '16 at 22:18
  • I guess `lm` should work fine with `combn`. You'll probably just want to reorder the columns rather than try to pass their names to the `~` formula, though, like `lm(ChickWeight[c(1,2)])$coef`. Depending on the application, I might just use a loop (to simplify things) or parallelization (if the problem is so big that it's slow to do sequentially). – Frank Jun 14 '16 at 22:35