[R] How to read an excel data into R?
Spencer Graves
spencer.graves at pdf.com
Thu Jun 23 22:28:49 CEST 2005
'RSiteSearch' is an R command new with R 2.0.0 or 2.1.0, I believe.
It essentially passes the argument string to "www.r-project.org" ->
Search -> "R site search". Consequently, it requires internet access to
work. When I have an R (or S-Plus) question for which I do not already
know where to find the answer, "R site search" has been my primary
search tool for some time.
spencer graves
Ling Jin wrote:
> Could you be more specific about RSiteSearch("read excel")? I think it
> must be useful.
>
################################
Your error message tells me that you have different numbers of fields
in different lines. You say you, "copied and pasted the excel data into
a txt file". I usually copy what I want into a clean sheet then File ->
Save, then File -> "Save As" -> "Save as type" = "CSV (Comma delimited)
(*.csv)" or "Text (Tab delimited) (*.txt)". Excel will ask if I'm sure
a couple of times, and I say yes. If that's what you've done and still
have a problem, then I have other tools:
First, I'll assign the file name to something like "File". Then,
'readLines(File, n=9)' tells me if the file starts as I think it does.
If I've got extra headers, it will tell me that.
Then, I do something like the following:
n.flds <- count.fields(File, sep="\t")
plot(n.flds)
sd(n.flds)
Then I play with the arguments to "count.fields" until 'sd(n.flds)'
is 0. Then I use "read.table" with arguments as I used to get
everything right in 'count.fields'. If I can't get sd(n.flds) to 0, you
can try read.table with 'fill=TRUE'. However, when you do that, you
need to check to make sure all the columns line up correctly with the
shorter lines.
Also, this issue has been discussed many times. 'RSiteSearch("read
excel")' just produced 1196 hits for me. If the above doesn't work, you
might try skimming a few from that list.
hope this helps.
spencer graves
Ling Jin 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
>
> ______________________________________________
> R-help at 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
--
Spencer Graves, PhD
Senior Development Engineer
PDF Solutions, Inc.
333 West San Carlos Street Suite 700
San Jose, CA 95110, USA
spencer.graves at pdf.com
www.pdf.com <http://www.pdf.com>
Tel: 408-938-4420
Fax: 408-280-7915
More information about the R-help
mailing list