Reputation: 539
so I just started using R and I have to struggle every now and then. I have this vector of stimuli names such as:
1. abc1.jpg
2. abc2.jpg
3. bcd1.jpg
4. bcd2.jpg
5. cde1.jpg
6. cde2.jpg
now the first entry corresponds to the picture on the left, and the second on the right. I want to create two vectors named "left" and "right", in which the "left" vector would consist of the entries 1,3,5,7,9... (goes until 300) and the right being always the second entry of the same picture (2,4,6...)
How do I do this? thanks in advance!
Upvotes: 2
Views: 100
Reputation: 81683
vec <- 1:10 # an example vector with the numbers from 1 to 10
vec[c(TRUE, FALSE)]
# [1] 1 3 5 7 9
vec[c(FALSE, TRUE)]
# [1] 2 4 6 8 10
The short index vector, e.g., c(TRUE, FALSE)
, is recycled until its length matches the length of the vector vec
.
Upvotes: 2
Reputation: 13103
x <- c('a','b','c','d','e','f','g','h')
x.odd <- x[(1:length(x) %% 2)==1] ; x.odd
#[1] "a" "c" "e" "g"
x.even <- x[(1:length(x) %% 2)==0] ; x.even
#[1] "b" "d" "f" "h"
Upvotes: 1