mapply basics? - how to create a matrix from two vectors and a function

橙三吉。 提交于 2019-11-28 13:36:24

Cannot be tested with the information offered, but this should work:

expGPA  <- outer(relGPA, avgGPA, FUN=f) # See below for way to make this "work"

Another useful function when you want to generate combinations is expand.grid and this would get you the "long form":

expGPA2 <-expand.grid(relGPA, avgGPA)
expGPA2$fn <- apply(expGPA2, 1, f)

The long form is what lattice and ggplot will expect as input format for higher level plotting.

EDIT: It may be necessary to construct a more specific method for passing column references to the function as pointed out by djhurio and (solved) by Sam Swift with the Vectorize strategy. In the case of apply, the sum function would work out of the box as described above, but the division operator would not, so here is a further example that can be generalized to more complex functions with multiple arguments. All the programmer needs is the number of the column for the appropriate argument in the "apply()"-ed" function, because (unfortunately) the column names are not carried through to the x argument:

> expGPA2$fn <- apply(expGPA2, 1, function(x) x[1]/x[2])
> str(expGPA2)
'data.frame':   48 obs. of  3 variables:
 $ Var1: num  -1.5 -1.3 -1.1 -0.9 -0.7 ...
 $ Var2: num  -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 ...
 $ fn  : num  0.75 0.65 0.55 0.45 0.35 ...
 - attr(*, "out.attrs")=List of 2
  ..$ dim     : int  16 3
  ..$ dimnames:List of 2
  .. ..$ Var1: chr  "Var1=-1.5" "Var1=-1.3" "Var1=-1.1" "Var1=-0.9" ...
  .. ..$ Var2: chr  "Var2=-2" "Var2= 0" "Var2= 2"

Edit2: (2013-01-05) Looking at this a year later, I realized that SamSwift's function could be vectorized by making its body use "+" instead of sum:

 1/(1+exp( relGPA*pred.model$coef[1] + avgGPA*pred.model$coef[2]) # all vectorized fns
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!