[R] reading Excel file

Earl F. Glynn efg at stowers-institute.org
Tue Mar 25 19:36:06 CET 2008


"Utkarsh Singhal" <utkarshs at ambaresearch.com> wrote in message 
news:A36876D3F8A5734FA84A4338135E7CC3033BF22A at BAN-MAILSRV03.Amba.com...
> Hi R,

> I have an excel file in which the third column is "date" and others are
> "character" and "numeric".
>
> If I use this to read the file in R:   x = read.xls("D:\\file.xls")
>
> The problem is that my date column is read in julian dates.

RDOBC has its problems when all data in a column are not exactly the same 
data type, but can read dates:

Consider an Excel file with the following: (view with fixed-width font):

Index   Label1  Date        Number   Label2  Mixed1  Mixed2
1       A       3/1/2008     12.45   X       1       A
2       B       3/2/2008     14.76   Y       2       B
3       C       3/3/2008     10.99   Z       A       1
4       D       3/4/2008     3.14            B       2

Use this to read the excel worksheet "Sheet1":

library(RODBC)
connection <- odbcConnectExcel("C:/temp/Sample.xls")
d <- sqlFetch(connection, "Sheet1")
odbcClose(connection)

d
class(d$Date)

> d
  Index Label1       Date Number Label2 Mixed1 Mixed2
1     1      A 2008-03-01  12.45      X      1     NA
2     2      B 2008-03-02  14.76      Y      2     NA
3     3      C 2008-03-03  10.99      Z     NA      1
4     4      D 2008-03-04   3.14   <NA>     NA      2
> class(d$Date)
[1] "POSIXt"  "POSIXct"


efg
Earl F. Glynn
Bioinformatics
Stowers Institute for Medical Research



More information about the R-help mailing list