[R] How to read an excel data into R?
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Thu Jun 23 09:29:41 CEST 2005
William Revelle <lists at 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 at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list