I have this sparse matrix I named N:
4 x 4 sparse Matrix of class "dgCMatrix" C1 C2 C3 C4 V1 . 3 5 2 V2 . 5 1 . V3 . . . . V4 . . 4 .
I am trying to delete rows with two or more missing values. I expect the result to be:
C1 C2 C3 C4 V1 . 3 5 2
I wrote this piece of code:
#iterate on rows and count: #how many values in row ri are bigger than 0 # if count is not bigger than limit, remove row ri limit = 3 for(ri in 1:nrow(N)){ count <- length(which(N[ri,]>0)) if (count <limit){ tmp <- paste("V",ri,sep="") rmv <- paste (rmv, tmp, sep= " ") } } #now remove specific row names N <- N[!rownames(N) %in% rmv, ]
The problem is that this does not work, because rmv is not specified in the first loop, and I get an error:
"object 'rmv' not found"
How can i initialize rmv? If I use:
rmv <- ""
Then I get a line that starts with empty space, for example:
> rmv [1] " V2"
and then my last line does not work:
N <- N[!rownames(N) %in% rmv, ]
Also, this is the very first code I've ever written in R, so if there is anything important that I am missing in the basic concepts that I would like to read (it took me 6 hours and read a lot in stackoverflow and various R tutorials, but I'm very proud to get to this, this is my first question).
Thanks!