-2

This is an example. Let's assume we have big dataset:

Numbers <- c(45, 67, 89, 45, 34, 65, 90, 99, 83, 88)

I want to use seq function to get 1) even numbers 2) odd numbers

camille
  • 16,432
  • 18
  • 38
  • 60
Narimanoglu
  • 61
  • 1
  • 5
  • 1
    You mean to separate those numbers into even and odd? I'm not sure what `seq` would do for you. `Numbers %% 2 == 0` tells you whether a number is even (if this returns true) or odd (if returns false) – camille Sep 13 '21 at 16:00
  • Odd numbers - like their position: 45, 89, 34, 90, 83 – Narimanoglu Sep 13 '21 at 16:01
  • Even numbers - again their even position, 67, 45, 65, 99, 88 – Narimanoglu Sep 13 '21 at 16:01
  • Numbers[c(TRUE, FALSE)] #Odd positioned numbers Numbers[c(FALSE, TRUE)] #Even positioned numbers – Narimanoglu Sep 13 '21 at 16:05
  • 1
    The fact that you want to extract numbers by the evenness/oddness of their *index*, not their own value, wasn't at all clear from the question. In that case, do the same modulo calculation I showed on the index – camille Sep 13 '21 at 16:05
  • Now, I just want to use seq to find these numbers. I hope it clarifies – Narimanoglu Sep 13 '21 at 16:05
  • why do you want to use `seq` ? – D.J Sep 13 '21 at 16:09
  • just saw: already answered: https://stackoverflow.com/questions/13461829/select-every-other-element-from-a-vector – Pax Sep 13 '21 at 16:14

1 Answers1

1

If you want to / have to use seq, you can do:

# Get numbers at odd indices (1, 3, 5, ...)
Numbers[seq(from = 1, to = length(Numbers), by = 2)]
#> [1] 45 89 34 90 83

# Get numbers at even indices (2, 4, 6, ...)
Numbers[seq(from = 2, to = length(Numbers), by = 2)]
#> [1] 67 45 65 99 88
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87