Grant Gillis
2012-Mar-20 16:33 UTC
[R] Remove individual rows from a matrix based upon a list
Dear All, 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. I can do this by hand removing rows based upon ids however I would like specify individuals to be removed from a list (lots of data). An example matrix: MyMatrix E985047 E985071 E985088 F952477 F952478 J644805 J644807 J644813 E985047 1 0.09 0 0 0 0 0 0.4 E985071 0.09 1 0 0 0 0 0 0.07 E985088 0 0 1 0 0 0 0.14 0 F952477 0 0 0 1 0.38 0 0 0 F952478 0 0 0 0.38 1 0 0 0 J644805 0 0 0 0 0 1 0.07 0 J644807 0 0 0.14 0 0 0.07 1 0 J644813 0.4 0.07 0 0 0 0 0 1 Example list of individuals to be removed MyList E985088 F952477 F952478 If I were to do this by hand it would look like MyMat1 <- MyMatrix[!rownames(MyMatrix)%in% "E985088",] colSums(MyMat1) MyMat2 <- MyMatrix[!rownames(MyMatrix)%in% " F952477 ",] colSums(MyMat2) MyMat3 <- MyMatrix[!rownames(MyMatrix)%in% " F952478 ",] colSums(MyMat3) How might I replace the individual ids (in quotes) with a list and remove rows corresponding to that list from the matrix for the calculation and returning the row to the list after each calculation before the next. I hope I've been clear!! [[alternative HTML version deleted]]
Rui Barradas
2012-Mar-20 20:24 UTC
[R] Remove individual rows from a matrix based upon a list
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.