Filtering a matrix
I have a list of full matrices and each matrix looks like,
P V E T R L K A -
P 17 0 1 0 0 0 0 0 0
S 3 0 2 0 1 1 1 0 0
O 2 0 0 1 0 0 0 0 1
V 0 2 0 0 0 0 0 1 0
M 0 3 0 0 0 0 0 0 0
L 3 0 0 0 0 0 0 0 0
C 1 0 0 0 0 0 0 0 0
After filtering for certain conditions , i am left with a list of 2*2 matrices, where each looks like:
P E
P 17 1
S 3 2
Fr开发者_开发技巧om the full matrix , i need to select only columns in 2*2 matrix. how will i select it?
The question is not entirely clear, but it seems to me that you are trying to find a way of extracting certain columns from your full matrix. The columns to extract are the ones that are in the small matrix, so in this case extract columns P
and E
.
Here is how to do this. First, use colnames
to find the names of the columns in your small matrix.
colnames(sub)
[1] "P" "E"
Then use array subsetting to extract these columns from the full matrix:
full[, colnames(sub)]
P E
P 17 1
S 3 2
O 2 0
V 0 0
M 0 0
L 3 0
C 1 0
Your data is:
full <- structure(c(17L, 3L, 2L, 0L, 0L, 3L, 1L, 0L, 0L, 0L, 2L, 3L,
0L, 0L, 1L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L,
0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L,
0L, 0L, 0L), .Dim = c(7L, 9L), .Dimnames = list(c("P", "S", "O",
"V", "M", "L", "C"), c("P", "V", "E", "T", "R", "L", "K", "A",
"X.")))
sub <- structure(c(17L, 3L, 1L, 2L), .Dim = c(2L, 2L), .Dimnames = list(
c("P", "S"), c("P", "E")))
精彩评论