# odd, even indices of a vector

4 messages
Open this post in threaded view
|

## odd, even indices of a vector

 Might be a trivial question but how to identify the odd and even indices of a vector? x = c(1,z,w,2,6,7) el of odd indices= 1,w,6 el of even indices= z,2,7 given the def of odd and even in https://stat.ethz.ch/pipermail/r-help/2010-July/244299.htmlshould a loop be used? for (i in 1: length(x)) if (is.odd(i)) print (i) Carol         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: odd, even indices of a vector

 ## these functions assume the argument is integer odd <- function(x) x%%2 != 0 even <- function(x) x%%2 == 0 evenb <- function(x) !odd(x) odd(1:10) even(1:10) evenb(1:10) On Mon, Jul 21, 2014 at 4:33 PM, carol white <[hidden email]> wrote: > Might be a trivial question but how to identify the odd and even indices of a vector? > > x = c(1,z,w,2,6,7) > > el of odd indices= 1,w,6 > el of even indices= z,2,7 > > given the def of odd and even in https://stat.ethz.ch/pipermail/r-help/2010-July/244299.html> > should a loop be used? > > for (i in 1: length(x)) > if (is.odd(i)) print (i) > > > Carol > >         [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.