Re: [R] element wise conditional assignment in r

2010-10-02 Thread yunjiangster
Phil, That's very elegant, and certainly solves my problem. But in order to assign the value to v, I would do v=ifelse(r==1,v-1,v). Is it possible to do the assignment inside the conditional? That way I can save some running time of assigning v to itself. Thanks for your solution! Sincerely,

[R] element wise conditional assignment in r

2010-10-01 Thread yunjiangster
Hi, I am looking for a one-liner code for the following situation say I have a vector r of 0 and 1's, and another vector v of equal length. I want to assign v[i]-1 to v[i] if r[i]=1, for all i. How would I do that using mapply or any other batch processing keywords? thanks. John -- View this

[R] How to apply vector value function to a multidimensional array indexed by the remaining dimensions?

2010-10-01 Thread yunjiangster
Hi, I am looking for some generalization of colSums and rowSums for general vector valued functions, and for arrays of more than 2 dimensions. So as a concrete example, suppose I have a 3 dimensional array, given by x = array(1:100,c(3,4,5)). and I want to sum the 3rd index of x to obain a 3