I have a 1000×1 vector (1000 rows and 1 column). I want to get elements in pairs (row 1 and row 2, row 3 and row 4, row 5 and row 6, etc.)
Here’s what I have so far
for (j in 1: ncol(total_loci)){
for (i in 1: sample_size){
# a pair
genotype[i]<- paste(total_loci[i, j], total_loci[i+1,j], sep="")
}
}
Genotype should thus be a 500×1 vector (500 rows and 1 column) containing the genotype. Assume that my for-loops are correct. I think my I needs to skip every other index — so my i should start at 1 then 3, 5, 7, 9, etc. The variable total_loci is of class data frame.
You should try to use vectorized solutions where possible. They’re usually more memory efficient and faster than loops.
In this case, you can use
seqto generate an index vector for every other element. Then you can use that index vector to subset the original vector in pairs.