[R] How to read a text table? A bit of trouble with Using R - An Introduction
hzi
hzi at uol.com.br
Tue Feb 27 23:13:15 CET 2001
Hello everyone!
Here´s a *truly newbie question*! (Oh, and BTW, I did read the
documentation and the mailing list archive)
I´m having a bit of difficulty regarding J. Maindonald booklet (pdf)
"Data Analysis and Graphics Using R - An Introduction"). It starts off with
the task to get some data on a text file (austpop.txt).
> austpop <- read.table("a:/austpop.txt", header=T)
Ok...So I actually *typed* the table with WordPad, using the Tab button
in order to separate the columns, and saved it in rich text format. As you
might have guessed, it didn´t work. I got a:
Error in count.fields(file, sep, quote, skip, blank.lines.skip) :
string terminated by newline or EOF
So I went back, read the R Data Import/Export documentation, and came to
the conclusion that the problem might be that other pieces of information
were missing. So I input:
> austpop <- read.table("C:/R/austpop.txt", header=T, row.names=1,
quote="", sep="\n", blank.lines.skip=TRUE)
and then (when everything seemed OK, since I got no error message):
> austpop
and got:
NULL data frame with 11 rows
*Then* I visited JM´s page (http://room.anu.edu.au/~johnm/) and saw a
link that I thought might be just what I needed: Data sets for "Using R...".
In a directory I found austpop.R which when clicked on led to the
following:
"austpop" <-
structure(list(Year = c(1917, 1927, 1937, 1947, 1957, 1967, 1977,
1987, 1997), NSW = c(1904, 2402, 2693, 2985, 3625, 4295, 5002,
5617, 6274), Vic. = c(1409, 1727, 1853, 2055, 2656, 3274, 3837,
4210, 4605), Qld = c(683, 873, 993, 1106, 1413, 1700, 2130, 2675,
3401), SA = c(440, 565, 589, 646, 873, 1110, 1286, 1393, 1480
), WA = c(306, 392, 457, 502, 688, 879, 1204, 1496, 1798), Tas. = c(193,
211, 233, 257, 326, 375, 415, 449, 474), NT = c(5, 4, 6, 11,
21, 62, 104, 158, 187), ACT = c(3, 8, 11, 17, 38, 103, 214, 265,
310), Aust. = c(4941, 6182, 6836, 7579, 9640, 11799, 14192, 16264,
18532)), .Names = c("Year", "NSW", "Vic.", "Qld", "SA", "WA",
"Tas.", "NT", "ACT", "Aust."), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9"))
In the site´s home page, there was something that read like this:
All data sets, other than those that are available from one of the R
libraries, are in the file dsets.R that is in the directory dsets. Use a
command of the form source("dsets.R") to import them into the R working
directory.
I cut and pasted austpop.R text content and saved it as...austpop.txt in
my working directory.
Then, I went for the kill :-)
> austpop <- read.table("C:/R/austpop.txt", header=T)
but it read:
row.lens=
[1] 9 11 11 12 12 15 14 15 11 14 15 16
Error in read.table("C:/R/austpop.txt", header = T) :
all rows must have the same length.
Ok, it seems I got all the pieces of the puzzle, right?
How do I go about putting them together? What do I do with all of these?
Help is *greatly* appreciated!
Thank you,
Best regards,
Henry L., in ding-state (in dire need of guidance)
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list