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

William Revelle
Thu, 23 Jun 2005 02:50:07 -0000

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)

Bill


At 8:38 PM -0400 6/22/05, Wensui Liu wrote:
>Ling,
>
>You might take a look at the function read.xls() in gdata library.
>
>HTH.
>
>
>On 6/22/05, Ling Jin <ljin at lbl.gov> wrote:
>>  Hi all,
>>
>>  Does anybody know the easiest way to import excel data into R? I
copied
>>  and pasted the excel data into a txt file, and tried read.table, but R
>>  reported that
>>
>>  Error in read.table("data_support.txt", sep = " ",
header = T) :
>>           more columns than column names
>>
>>  Thanks!
>>
>  > Ling
>>


--
William Revelle		http://pmc.psych.northwestern.edu/revelle.html
Professor			http://personality-project.org/personality.html
Department of Psychology       http://www.wcas.northwestern.edu/psych/
Northwestern University	http://www.northwestern.edu/