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