mapply基础知识? -如何从两个向量和一个函数创建矩阵 [英] mapply basics? - how to create a matrix from two vectors and a function
问题描述
我正在尝试创建一个data.frame来从中创建图形.我有一个函数和两个向量要用作两个输入.这有点简化,但是基本上我只有:
I am trying create a data.frame from which to create a graph. I have a function and two vectors that I want to use as the two inputs. This is a bit simplified, but basically all I have is:
relGPA <- seq(-1.5,1.5,.2)
avgGPA <- c(-2,0,2)
f <- function(relGPA, avgGPA) 1/(1+exp(sum(relGPA*pred.model$coef[1],avgGPA*pred.model$coef[2])))
我想要的只是一个data.frame,其中avgGPA值具有3列,而relGPA值具有16行,并在单元格中具有结果值.
and all I want is a data.frame with 3 columns for the avgGPA values, and 16 rows for the relGPA values with the resulting values in the cells.
我很抱歉这很基本,但是我向您保证,在没有您帮助的情况下,我试图做到这一点.我尝试按照sapply和mapply手册页上的示例进行操作,但是对于R来说,我有点太新了,无法了解我要做什么.
I apologize for how basic this is, but I assure you I have tried to make this happen without your assistance. I have tried following the examples on the sapply and mapply man pages, but I'm just a little too new to R to see what I'm trying to do.
谢谢!
推荐答案
无法使用所提供的信息进行测试,但这应该可以:
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"
要生成组合时,另一个有用的功能是expand.grid
,这将使您获得长格式":
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)
长格式是将晶格和ggplot作为更高级别绘图的输入格式所期望的形式.
The long form is what lattice and ggplot will expect as input format for higher level plotting.
也许有必要构造一种更具体的方法,以将列引用传递给djhurio和Sam Swift指出的(通过解决方案)使用Vectorize
策略的函数.在apply
的情况下,sum
函数将如上所述开箱即用,但是除法运算符将无法工作,因此这里是另一个示例,可以将其推广到具有多个参数的更复杂的函数.程序员需要的只是"apply()"-ed函数中适当参数的列号,因为(不幸的)列名没有传递给x
参数:
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)一年后,我意识到,可以通过使主体使用"+"代替sum
来对SamSwift的函数进行矢量化处理:
(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
这篇关于mapply基础知识? -如何从两个向量和一个函数创建矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!