[R] Remove individual rows from a matrix based upon a list

Rui Barradas rui1174 at sapo.pt
Tue Mar 20 21:24:03 CET 2012


Hello,

>
> Thanks in advance for any help.  I have a square matrix of measures of
> interactions among individuals and would like to calculate a values from a
> function (colSums for example) with a single individual (row) excluded in
> each instance.  That individual would be returned to the matrix before the
> next is removed and the function recalculated. 
> 

Try


MyMatrix <- structure(list(V2 = c(1, 0.09, 0, 0, 0, 0, 0, 0.4), V3 = c(0.09, 
1, 0, 0, 0, 0, 0, 0.07), V4 = c(0, 0, 1, 0, 0, 0, 0.14, 0), V5 = c(0, 
0, 0, 1, 0.38, 0, 0, 0), V6 = c(0, 0, 0, 0.38, 1, 0, 0, 0), V7 = c(0, 
0, 0, 0, 0, 1, 0.07, 0), V8 = c(0, 0, 0.14, 0, 0, 0.07, 1, 0), 
    V9 = c(0.4, 0.07, 0, 0, 0, 0, 0, 1)), .Names = c("V2", "V3", 
"V4", "V5", "V6", "V7", "V8", "V9"), class = "data.frame", row.names =
c("E985047", 
"E985071", "E985088", "F952477", "F952478", "J644805", "J644807", 
"J644813"))

MyList <- c("E985088", "F952477", "F952478")


inx <- which(rownames(MyMatrix) %in% MyList)

result <- lapply(inx, function(i) colSums(MyMatrix[-i, ]))

# Not needed, but makes what is what more clear 
names(result) <- paste("Without", MyList, sep=".")
result

>
> I hope I've been clear!! 
>

I believe you were, but your data is a mess.
The structures above were produced with function 'dput', it makes it much,
much easier to create the objects.
See

?dput

and use it!

Hope this helps,

Rui Barradas


--
View this message in context: http://r.789695.n4.nabble.com/Remove-individual-rows-from-a-matrix-based-upon-a-list-tp4489462p4490257.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list