[R] extracting rows and columns from a big matrix

arun smartpink111 at yahoo.com
Sun Jul 15 22:22:20 CEST 2012


Hello,

Try this:

dat1<-read.table(text="
  X1 X7 X12 X15 X22 X26 X31 X34 X39 X44 X51
X1  1  2   3   4  5  6  7  8  9 10  11
X7  11  9  7  5   3  1 10 8 6  4  2
X12 3  4  7  8  5   7  2  9  1  3  2
X15 9  9  8  4  7  1   1  3  2  5  3
X22 6  7  7  4  4  2  9  8  8  1  1
X26 3  9  4  8  5  7  6  1  2  3  8
X31 1  2  1  3  1  4  1  5  1  6  1
X34 6  7  8  5  2  9  5  1  6  8  9
X39 4  8  7  4  6  5  1  9  2  7  5
X44 2  2  2  8  6  7  9  5  3  7  7
X51 9  9  9  6  6  4  8  7  2  1  3
",sep="", header=TRUE)

#Inorder to get your final submatrix:
#Either this:
 dat1[c(1,3,5,7),c(1,3,5,7)]


# or
dat1[(select=c("X1","X12","X22","X31")),(select=c("X1","X12","X22","X31"))]
    X1 X12 X22 X31
X1   1   3   5   7
X12  3   7   5   2
X22  6   7   4   9
X31  1   1   1   1


#You can convert this data.frame to matrix
dat2<-as.matrix(dat1[(select=c("X1","X12","X22","X31")),(select=c("X1","X12","X22","X31"))])
 is.matrix(dat2)
[1] TRUE



A.K.







----- Original Message -----
From: A J <anxusgo at hotmail.com>
To: jholtman at gmail.com
Cc: r-help at r-project.org
Sent: Sunday, July 15, 2012 3:43 PM
Subject: Re: [R] extracting rows and columns from a big matrix


Sorry so much for mistakes. 

It was an example code and I commited some mistakes typing it. But meaning the original code is right (I have checked several times) I am not sure about how to solve the problem of extracting columns and rows using labels from a squared matrix. I have enclosed a text file with the idea in order to understand it better.

Thanks again, and sorry for the inconvenience.

Best,

AJ



> Date: Sun, 15 Jul 2012 14:53:47 -0400
> Subject: Re: [R] extracting rows and columns from a big matrix
> From: jholtman at gmail.com
> To: anxusgo at hotmail.com
> CC: r-help at r-project.org
> 
> For a start, you are missing a quote and a parenthese on the
> statement; probably should be: (another quote was also missing)
> 
> n<-subset(m, select=c("X1", "X7", "X12","X15", "X22", "X26", "X31",
> "X34", "X39", "X44", "X51", "X58"))
> 
> Not sure what you want with the rownames; an example would help and
> post with 'dput'.
> 
> On Sun, Jul 15, 2012 at 2:47 PM, A J <anxusgo at hotmail.com> wrote:
> >
> > Hi there and thanks in advance.
> >
> > I have a large symmetrical matrix stored in a text file. After load in R I would like to extract the same number of columns and rows (symmetrical submatrix) using their labels.
> >
> > I have tried this code in order to extract columns, but R console gives me the "+" sign at the end of the code, pointing out incomplete command, so it is not working:
> >
> > m<-read.table("C:/backup/symmetrical.csv")
> >
> > n<-subset(m, select=c("X1", "X7", "X12", X15", "X22", "X26", "X31", "X34", "X39", "X44", "x51", "X58)
> >
> > Therefore, I have no tried with row names yet.
> >
> > Any suggestions? Sorry for the inconvenience. I have read some information about this but always have the same problem with "+" and I do not have any idea to follow.
> >
> > Best,
> >
> > AJ
> >
> >
> >
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> 
> 
> 
> -- 
> Jim Holtman
> Data Munger Guru
> 
> What is the problem that you are trying to solve?
> Tell me what you want to do, not how you want to do it.
                          
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list