odd, even indices of a vector

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

odd, even indices of a vector

carol white
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.
Reply | Threaded
Open this post in threaded view
|

Re: odd, even indices of a vector

Richard M. Heiberger
## 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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: odd, even indices of a vector

Bert Gunter
In reply to this post by carol white
Ms. White:

Unless I have seriously misjudged, you really really really need to go
through an R tutorial -- An Intro to R ships with R, but there are
many on the web -- before posting here further. You do not appear to
have made much of an effort to learn even the basics, and I consider
it unfair to post questions here on basic matters that a tutorial
would tell you about.

And, of course, my apologies if I have gotten it wrong.

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll




On Mon, Jul 21, 2014 at 1: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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: odd, even indices of a vector

Hervé Pagès
In reply to this post by carol white
Hi Carol,

On 07/21/2014 01:33 PM, carol white 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

The easiest way is to subset your vector with c(TRUE, FALSE) to keep
only the odd indices:

   > letters[c(TRUE, FALSE)]
   [1] "a" "c" "e" "g" "i" "k" "m" "o" "q" "s" "u" "w" "y"

and with c(FALSE, TRUE) to keep only the even indices:

   > letters[c(FALSE, TRUE)]
   [1] "b" "d" "f" "h" "j" "l" "n" "p" "r" "t" "v" "x" "z"

Note that the above doesn't work properly with vector of length < 2.

A method that works independently of the length of the vector (using
the is.odd and is.even functions from the "identifying odd or even
number" post that you referred to):

   x[is.odd(seq_along(x))]

   x[is.even(seq_along(x))]

Hope this helps,
H.

>
> 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.
>

--
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: [hidden email]
Phone:  (206) 667-5791
Fax:    (206) 667-1319

______________________________________________
[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.