In RStudio how to choose elements of a vector that are in odd or even positions?
In RStudio how to choose elements of a vector that are in odd or even positions?
You can create two indexes that identify even and odd positions
x <- rnorn(1500)
par <- (1:length(x) %% 2) == 0
impar <- (1:length(x) %% 2) != 0
Then just call the vector with these indexes
x[par]
x[impar]
One way is to take advantage of the recycling that happens automatically in R. Example:
x <- 1:15
x[c(TRUE, FALSE)] # retorna os ímpares
x[c(FALSE, TRUE)] # retorna os pares
Remembering that indexes start from 1 in R.