[R] Beginner problem - using mod function to print odd numbers

Rolf Turner r@turner @end|ng |rom @uck|@nd@@c@nz
Sun Jun 6 23:27:18 CEST 2021


On Sun, 6 Jun 2021 04:34:02 -0700
William Michels via R-help <r-help using r-project.org> wrote:


<SNIP>

> ... But of course, a more simple approach than I previously
> posted would be below (although less idiomatic than your answer):
> 
> > object <- 1:100
> > index <- ifelse(object %% 2 == 1, TRUE, FALSE)
> > object[index]
>  [1]  1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43
> 45 47 49 51 53 55 57
> [30] 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99

Why faff around with ifelse()?

    object[object %% 2 == 1]

works just fine.

<SNIP>

cheers,

Rolf Turner

-- 
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276



More information about the R-help mailing list