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 (x*x) } w <- applyfun(v, mysquare, 2) then w should be c(1, 4, 3, 16) Thanks for your time, Michael Bach
Good morning Michael, On Thu, Apr 5, 2012 at 7:01 AM, Michael Bach <phaebz at 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 functions, but > found no hint.Use indexing, or ifelse.> > For example: > > v <- c(1, 2, 3, 4) > mysquare <- function (x) { return (x*x) } > w <- applyfun(v, mysquare, 2) > > then w should be c(1, 4, 3, 16)Here is one way: w <- ifelse(v %in% seq(1, length(v), 2), v, v^2) Best, Ista> > Thanks for your time, > Michael Bach > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Michael Bach <phaebz <at> gmail.com> writes:> how do I e.g. square each second element of avector with an even> number of elements? Or more generally toapply a function to every> 'nth' element of a vector. I looked into theapply functions, but> found no hint. > For example: > v <- c(1, 2, 3, 4) > mysquare <- function (x) { return (x*x) } > w <- applyfun(v, mysquare, 2) > then w should be c(1, 4, 3, 16) > Michael BachHi Michael, v^(2 - seq_along(v) %% 2) [1] 1 4 3 16 Ken -- Ken Knoblauch Inserm U846 Stem-cell and Brain Research Institute Department of Integrative Neurosciences 18 avenue du Doyen L?pine 69500 Bron France tel: +33 (0)4 72 91 34 77 fax: +33 (0)4 72 91 34 61 portable: +33 (0)6 84 10 64 10 http://www.sbri.fr/members/kenneth-knoblauch.html
David Winsemius
2012-Apr-05 13:53 UTC
[R] Apply function to every 'nth' element of a vector
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 <- c(1, 2, 3, 4) > mysquare <- function (x) { return (x*x) } > w <- applyfun(v, mysquare, 2) > > then w should be c(1, 4, 3, 16)An alternate approach to the ifelse() solutions you have been offered is to use identical indexing on both sides of an assignment function. > v <- c(1, 2, 3, 4) > w <-v > w[seq(2, length(w), by =2)] <-w[seq(2, length(w), by =2)]^2 > w [1] 1 4 3 16 If you still wanted to create a function that would square each element "in place" you could do this with an indexing strategy: v <- c(1, 2, 3, 4) w <-v mysqr <- function (x) { eval.parent(substitute(x <- x^2)) } mysqr(w[seq(2, length(w), by =2)]) w #[1] 1 4 3 16 (Credit: http://www.statisticsblog.com/2011/10/waiting-in-line-waiting-on-r/ and search on 'inc')> > Thanks for your time, > Michael Bach > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT