Re: [R] efficient way to fill up matrix (and evaluate function)

2011-11-28 Thread jim holtman
For the example of the function you gave, it is already 'vectorized': myfunc - function(x1, x2) { + x1 + x2 + } myfunc(1:10, 1:10) [1] 2 4 6 8 10 12 14 16 18 20 outer(1:10, 1:10, myfunc) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,]2345678

[R] efficient way to fill up matrix (and evaluate function)

2011-11-27 Thread Sachinthaka Abeywardana
Hi All, I want to do something along the lines of: for (i in 1:n){ for (j in 1:n){ A[i,j]-myfunc(x[i], x[j]) } } The question is what would be the most efficient way of doing this. Would using functions such as sapply be more efficient that using a for loop? Note that n can be a

Re: [R] efficient way to fill up matrix (and evaluate function)

2011-11-27 Thread jim holtman
Take a look at 'outer' and vectorized your function. Also look at 'expand.grid'. On Sunday, November 27, 2011, Sachinthaka Abeywardana sachin.abeyward...@gmail.com wrote: Hi All, I want to do something along the lines of: for (i in 1:n){ for (j in 1:n){ A[i,j]-myfunc(x[i],

Re: [R] efficient way to fill up matrix (and evaluate function)

2011-11-27 Thread Joshua Wiley
Hi Sachin, The technique you are suggesting is likely to be just as efficient as any other if indeed myfunc must be called on each x[i] x[j] element individually. I would take the additional steps of instantiating A as a matrix (something like: A - matrix(0, nrow = n, ncol = n) Depending, you

Re: [R] efficient way to fill up matrix (and evaluate function)

2011-11-27 Thread Sachinthaka Abeywardana
Hi Jim, What exactly do you mean by vectorized. I think outer looks like what I was looking for. BUT there was a (weighted) distance matrix calculation that I was trying to vectorize, which wasnt related to this post. Could you proved a bit more details as to what you were referring to, and maybe

Re: [R] efficient way to fill up matrix (and evaluate function)

2011-11-27 Thread Joshua Wiley
Here is an example, of course, this is predicated on how myfunc() behaves---if it could not handle adding a constant to a vector, things would choke: ## Current method myfunc - function(x1, x2) { x1 + x2 } x - 1:10 n - length(x) A - matrix(0, nrow = n, ncol = n) for (i in 1:n){ for (j in

Re: [R] Efficient way to fill a matrix

2008-11-07 Thread Philipp Pagel
Thank you Phil and Bert! I was sure there must be an efficient way using some kind of indexing trick but totally did not see the as.matrix solution. Thanks again Philipp On Wed, Nov 05, 2008 at 02:59:20PM -0800, Phil Spector wrote: Philipp - res = matrix(NA,5,3)

Re: [R] Efficient way to fill a matrix

2008-11-05 Thread stephen sefick
#reshape package should do it library(reshape) foo - data.frame(row=1:5, col=1:3, val=rnorm(15)) cast(foo, row~col) On Wed, Nov 5, 2008 at 5:47 PM, Philipp Pagel [EMAIL PROTECTED] wrote: Dear R experts, Suppose I have a data frame of three variables: foo - data.frame(row=1:5,

Re: [R] Efficient way to fill a matrix

2008-11-05 Thread Peter Dalgaard
Philipp Pagel wrote: Dear R experts, Suppose I have a data frame of three variables: foo - data.frame(row=1:5, col=1:3, val=rnorm(15)) foo row col val 11 1 -1.00631642 22 2 0.77715344 33 3 0.17358793 44 1 -1.67226988 55 2 1.08218836 61

Re: [R] Efficient way to fill a matrix

2008-11-05 Thread Bert Gunter
])] - foo[,3] See ?[ for details. -- Bert Gunter -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of stephen sefick Sent: Wednesday, November 05, 2008 2:52 PM To: Philipp Pagel Cc: r-help@r-project.org Subject: Re: [R] Efficient way to fill a matrix #reshape