Re: [R] How to read an excel data into R?

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Thu 23 Jun 2005 - 17:29:41 EST

William Revelle <lists@revelle.net> writes:

> Ling,
>
> If any column has text with spaces between words, this will lead
> to the "more columns ..." problem.
> Delete the spaces and try again.
>
> e.g., if the Excel file is
> Var1 Var2 Var3
> text 1 2
> more text 3 4
> yet more 5 6
> and more 7 8
> blahblah 9 10
>
> On a Mac, this will lead to the error message
> "Error in scan(file = file, what = what, sep = sep, quote = quote,
> dec = dec, :
> line 1 did not have 4 elements"
> (which I believe is the equivalent message to what you are getting on a PC)
>
> But, if your remove the blanks in column 1, this reads as
>
> > x <- read.table("test.txt",header=T)
> > x
> Var1 Var2 Var3
> 1 text 1 2
> 2 moretext 3 4
> 3 yetmore 5 6
> 4 andmore 7 8
> 5 blahblah 9 10
>
> with no error message.
>
> Alternatively, for small files, if using a PC try copying the Excel
> spreadsheet to your clipboard and
>
> x <- read.table(file("clipboard"), header = TRUE) or, if using a Mac
>
> x <- read.table(pipe("pbpaste"), header = TRUE)

PLEASE! There are functions read.csv(), and read.delim() specifically for the purpose of reading exported files. They have the options set exactly to handle issues of missing fields at end of line and embedded blanks. Do use them. It's all on the help page for read.table...

(read.csv2, read.delim2 in locales that use comma as decimal point)

-- 
   O__  ---- Peter Dalgaard             ุster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Thu Jun 23 17:57:36 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:58 EST