[R] Excel file Import - to Matrix format

David L Carlson dcarlson at tamu.edu
Mon Jul 23 17:47:50 CEST 2012


Use read.table() with the row.names= argument set to the column number that
represents the row.names (presumably 1) and then convert the data.frame to a
matrix:

A <- read.table(file="fname.csv", row.names=1)
B <- as.matrix(A)

If you are using Windows, you can open Excel, copy the data, Copy and then
change "fname.csv" to "clipboard-128" and R will read the data from the
Windows clipboard.

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352




> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of arun
> Sent: Saturday, July 21, 2012 8:16 PM
> To: biostat1
> Cc: R help
> Subject: Re: [R] Excel file Import - to Matrix format
> 
> Hello,
> 
> Try this:
> 
> You can use the read.table with sep="," if it is comma separated to
> read the file.
> 
> test1<-read.table(text="
> 0.141  0.242  0.342
> 0.224  0.342  0.334
> 0.652  0.682  0.182
> ",sep="",header=FALSE)
>  #Read data
> test1
>  #   V1    V2    V3
> #1 0.141 0.242 0.342
> #2 0.224 0.342 0.334
> #3 0.652 0.682 0.182
> 
> 
> #Convert to matrix as it is that you wanted
> test2<-as.matrix(test1)
> colnames(test2)<-NULL
> genelist<-c("Fkh2","Swi5","Sic1")
> rownames(test2)<-genelist
> test2
> #      [,1]  [,2]  [,3]
> #Fkh2 0.141 0.242 0.342
> #Swi5 0.224 0.342 0.334
> #Sic1 0.652 0.682 0.182
> 
> #2nd case: As in your example,
> test1<-read.table(text="
> IMAGE:152 0.141  0.242  0.342
> IMAGE:262 0.224  0.342  0.334
> IMAGE:342 0.652  0.682  0.182
> ",sep="",header=FALSE)
> 
> test2<-as.matrix(test1[-1])
> colnames(test2)<-NULL
> genelist<-c("Fkh2","Swi5","Sic1")
> rownames(test2)<-genelist
> test2
> #      [,1]  [,2]  [,3]
> #Fkh2 0.141 0.242 0.342
> #Swi5 0.224 0.342 0.334
> #Sic1 0.652 0.682 0.182
> 
> A.K.
> 
> 
> ----- Original Message -----
> From: biostat1 <sridiyer at gmail.com>
> To: r-help at r-project.org
> Cc:
> Sent: Saturday, July 21, 2012 7:29 PM
> Subject: [R] Excel file Import - to Matrix format
> 
> Hi,
> 
> New to R. Need a bit of help. Thanks
> 
> I am trying to import data from Excel file. The package I am trying to
> use
> requires data in Matrix format.
> 
> Excel -> R Matrix with the constraint that the very first column in
> Excel
> file should became the names for rows of the matrix.
> 
> Example. Data has 1000 rows and 11 columns.
> 1st column is the names of Genes
> 10 coulmns of numerical data.
> 
> I need to read this into R.
> it should be a 1000 (row) X 10 Column (Matrix)
> 1st column of Excel file becomes name of the rows.
> 
> I am experimenting with reading as data frame (as I am unable to
> find any info on reading into matrix)  split the data frame, etc.
> 
> Thanks truly appreciate your help.
> 
> What I need: http://r.789695.n4.nabble.com/file/n4637332/thisWorks2.png
> 
> http://r.789695.n4.nabble.com/file/n4637332/doesNotWork1.png
> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/Excel-file-
> Import-to-Matrix-format-tp4637332.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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.
> 
> 
> ______________________________________________
> 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