How does one handle infinite values in matrices in R?
我有一个矩阵,我怀疑它包含一些无限元素。
我有两个问题:
谢谢
尝试此操作,但请确保您的输入数据属于类矩阵:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | set.seed(1) # make data n <- 20 m <- 10 M <- matrix(rnorm(n*m), n, m) # add Infs M[sample(x = length(M), size = length(M)*0.1)] <- Inf image(seq(n), seq(m), M, xlab ="rows", ylab ="columns") # here is the vector that you want to multiply each row with multVec <- seq(m) # apply with removal of non-finite values res <- apply(M, 1, function(x){ tmp <- x * multVec sum(tmp[is.finite(tmp)]) }) |