问题
Is there a way to reference a part of a matrix within a for loop?
for (j in 1:x1)
for (k in 1:x2) {
matrix[j,8k-6:8k+1] <- AlleleFreq.t1[k,1:8]
}
}
I get an error message saying "unexpected symbol in "alldata.t1[j,8k". What is the correct syntax for preforming this sort of operation?
Thank you.
回答1:
use parens & *
to multiply:
8k-6:8k+1 ~~~> (8*k-6):(8*k+1)
the seq
operator :
takes precedence over arithmetic operators such as -
Thus, without parens, you have
(8*k) - c(6, 7, 8) + ((8*k) + 1)
来源:https://stackoverflow.com/questions/20553057/referencing-entries-of-matrix-in-r-within-for-loop