-3

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

From the full matrix , i need to select only columns in 2*2 matrix. how will i select it?

Andrie
  • 176,377
  • 47
  • 447
  • 496
Ram
  • 331
  • 1
  • 3
  • 11
  • 1
    If you have silently downvoted this question, please extend the courtesy to the OP of saying what the issues are and how to fix it. – Andrie Aug 10 '11 at 19:32
  • It's not obvious to me what you are trying to do. Can you please expand? Do you in this example wish to extract columns P and E from your full matrix? – Andrie Aug 10 '11 at 19:44
  • 1
    In addition to clarifying your question, you should make it reproducible, as per the guidance here http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example . Your past questions show a pattern of non-reproducibility, and you will get much better answers if you learn to post better questions :-) – Ari B. Friedman Aug 10 '11 at 20:44

1 Answers1

1

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")))
Andrie
  • 176,377
  • 47
  • 447
  • 496
  • Thank you very much and i would apologize everyone for not framing my question correctly and would make sure that my further questions would be reproducible. – Ram Aug 11 '11 at 17:56