rinformation-retrieval

Retrieving row and column names in R


So I have a matrix TMatrix that i'm cycling through, and I want to put the row and column names for every cell that contains a value that is not finite into a table. I've tried to doing the following, but I keep getting NA for the row and column names. What's going on?

    AA <- 1:rowlength
    BB <- 1:ncol(Nmatrix)
    for(i in AA){
        for(j in BB){
            if (is.finite(TMatrix[i,j])==FALSE){
                TNS <- matrix(data=NA,nrow=1,ncol=4)
                TNS[1,1] <- TMatrix[i,j]
                TNS[1,2] <- Nmatrix[i,j]
                TNS[1,3] <- paste(rownames(TMatrix)[TMatrix[i,j]])
                TNS[1,4] <- paste(colnames(TMatrix)[TMatrix[i,j]])
                TMinf <- rbind(TMinf,TNS)
            }
            PMatrix[i,j] <- pt(TMatrix[i,j],n1+n2-2)
        }
    }

Solution

  • Never mind, I figured it out. I had the index wrong. It should be like this:

    AA <- 1:rowlength
    BB <- 1:ncol(Nmatrix)
    for(i in AA){
        for(j in BB){
            if (is.finite(TMatrix[i,j])==FALSE){
                TNS <- matrix(data=NA,nrow=1,ncol=4)
                TNS[1,1] <- TMatrix[i,j]
                TNS[1,2] <- Nmatrix[i,j]
                TNS[1,3] <- rownames(TMatrix)[i]
                TNS[1,4] <- colnames(TMatrix)[j]
                TMinf <- rbind(TMinf,TNS)
            }
            PMatrix[i,j] <- pt(TMatrix[i,j],n1+n2-2)
        }
    }