0
我有一个有11217行和2列的矩阵1,第二个矩阵2有10行和10列。现在,我想比较矩阵1行中的值和矩阵2的索引,如果它们相同,那么矩阵2的相应索引(当前为0)的值应该增加+1。将矩阵的同一行的2个值与另一个矩阵的行和列索引相比较R
c1 <- x[2:11218] #these values go from 1 to 10
#second column from index 3 to N
c2 <- x[3:11219] #these values also go from 1 to 10
#matrix with column c1 and c2
m1 <- as.matrix(cbind(c1 = c1, c2 = c2))
#empty matrix which will count the frequencies
m2 <- matrix(0, nrow = 10, ncol = 10)
#change row and column names of m2 to the numbers of 1 to 10
dimnames(m2) <-list(c(1:10), c(1:10))
#go through every row of the matrix m1 and look which rotation appears, add 1 to m2 if the rotation
#equals the corresponding index
r <- c(1:10)
c <- c(1:10)
for (i in 1:nrow(m1)) {
if(m1[i,1] == r & m1[i,2] == c)
m2[r,c]+1
}
没有频率在哪里计算,我不明白为什么?
你可能要替换'M2 [最后一行r,c] = m2 [r,c] + 1' – etienne