[R] Apply function to every 'nth' element of a vector

2012-04-05 Thread Michael Bach
Dear R users, how do I e.g. square each second element of a vector with an even number of elements? Or more generally to apply a function to every 'nth' element of a vector. I looked into the apply functions, but found no hint. For example: v - c(1, 2, 3, 4) mysquare - function (x) { return

Re: [R] Apply function to every 'nth' element of a vector

2012-04-05 Thread Ista Zahn
Good morning Michael, On Thu, Apr 5, 2012 at 7:01 AM, Michael Bach pha...@gmail.com wrote: Dear R users, how do I e.g. square each second element of a vector with an even number of elements? Or more generally to apply a function to every 'nth' element of a vector. I looked into the apply

Re: [R] Apply function to every 'nth' element of a vector

2012-04-05 Thread ken knoblauch
Michael Bach phaebz at gmail.com writes: how do I e.g. square each second element of a vector with an even number of elements? Or more generally to apply a function to every 'nth' element of a vector. I looked into the apply functions, but found no hint. For example: v - c(1, 2, 3, 4)

Re: [R] Apply function to every 'nth' element of a vector

2012-04-05 Thread ken knoblauch
ken knoblauch ken.knoblauch at inserm.fr writes: Michael Bach phaebz at gmail.com writes: how do I e.g. square each second element of a vector with an even number of elements? Or more generally to apply a function to every 'nth' element of a vector. I looked into the apply

Re: [R] Apply function to every 'nth' element of a vector

2012-04-05 Thread David Winsemius
On Apr 5, 2012, at 7:01 AM, Michael Bach wrote: Dear R users, how do I e.g. square each second element of a vector with an even number of elements? Or more generally to apply a function to every 'nth' element of a vector. I looked into the apply functions, but found no hint. For example: v

Re: [R] Apply function to every 'nth' element of a vector

2012-04-05 Thread Michael Bach
Thank you very much for your comments Ista and David! I will experiment and see which one serves my needs best. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide