使用R如何使用向量中的值替换列中的值(或缺少值)?

时间:2016-11-18 04:05:08

标签: r

兄弟们,我得到了这个lil矩阵:

data <- c(1, 0, 2, 0, 4, 3, 3, 0, 1, 1, 0, 0, 2, 0, 4, 0, 1, 0, 0, 0, 1, 0, 0, 1, 8, 0, 3, 0, 5, 2, 0, 0, 0, 0, 0, 0)

matrixData <- matrix(data, nrow = 6, ncol = 6, byrow = TRUE)

diag(matrixData) <- 0 # zeros out the diag of the matrix
lilMatrix  = fractions(sweep(matrixData, 2, colSums(matrixData), FUN="/"))`
lilMatrix

      [,1] [,2] [,3] [,4] [,5] [,6]
 [1,]    0  NaN  2/7    0  4/5  1/2
 [2,] 3/13  NaN  1/7    1    0    0
 [3,] 2/13  NaN    0    0  1/5    0
 [4,]    0  NaN  1/7    0    0  1/6
 [5,] 8/13  NaN  3/7    0    0  1/3
 [6,]    0  NaN    0    0    0    0

我只是想用这些东西替换列[,2]中愚蠢的愚蠢NaN:

articleVector = fractions(c(3/14,2/14, 5/14, 1/14, 2/14, 1/14))

你们都能帮助兄弟吗?

0 个答案:

没有答案