Re: [R] Help on apply() function

2009-04-28 Thread jim holtman
In the function call in 'apply', you are passed the values in the rows: > set.seed(30) > z = matrix(rnorm(10), 5, 2) > apply(z, 1, function(x) sum(z[x,1]*1, z[x,2]*3)) [1] -0.908321 0.00 0.00 -5.822442 -5.822442 > z [,1] [,2] [1,] -1.2885182 -1.5113079 [2,] -0.3476894 0

Re: [R] Help on apply() function

2009-04-28 Thread Sarah Goslee
You are indeed missing something. x is _one row_ of z, not the index specifying that row. So what you want is: apply(z, 1, function(x)sum(x[1]*1, x[2]*3)) Thanks for including a reproducible example. Sarah On Tue, Apr 28, 2009 at 9:02 PM, megh wrote: > > Hi all, can you please clarify me what i

[R] Help on apply() function

2009-04-28 Thread megh
Hi all, can you please clarify me what is the wrong with following codes : set.seed(30) z = matrix(rnorm(10), 5, 2) apply(z, 1, function(x) sum(z[x,1]*1, z[x,2]*3)) However I can not get the desired result. For example, "sum(z[1,1]*1, z[1,2]*3)" gives -5.822442 which is actually correct. Am I mi