[R] Newb question re. read.table...
Rolf Turner
r.turner at auckland.ac.nz
Wed Mar 3 23:34:49 CET 2010
On 4/03/2010, at 10:50 AM, Steven DeRose wrote:
> I'm trying to get started with R, on Ubuntu. I worked through the
> tutorial, and have made a small tab-delimited subset of my data to try
> out (10 cases with about a dozens variables). But I can't seem to figure
> out how to actually refer to variables. I presume I'm missing something
> very simple, but quite a while searching the doc and the FAQ haven't
> helped me.
>
> I'm loading the data with
> con <- read.table("tiny.txt", header=TRUE)
>
> The first record is a set of variable names, tab-separated like the rest
> of the rows. There are no row labels, thus the same number of
> tab-delimited fields in the header record and the following records. The
> read.table returns silently, and I can get a reasonable summary(con).
> But if I try something like plot(rel,len), where rel and len are two of
> the labels from the header row, I get
>
> Error in plot(rel, len) : object 'rel' not found
>
> I've tried many variations (different variables, adding "con." on the
> front, quoting, using field numbers instead of names, etc. I've also
> read what I can find on read.table, but I'm clearly missing some basic
> thing....
>
> Can somebody put me back on the right track? Is there some additional
> thing I have to do to make this into a "real" frame, or to bind
> variables names to header names, or something like that?
>
> Thanks, and sorry for being dense....
You haven't got ``rel'' and ``len'' in your workspace. They are
columns (components) of the data frame object ``con'' which ***is***
in your workspace.
There are various ways to access components of a data frame:
* plot(con$rel,con$len)
* plot(con[["rel"]],con[["len"]])
* plot(con[,"rel"],con[,"len"])
* with(con, plot(rel,len))
You should read enough R documentation so that you understand the
under-pinnings of these various syntaxes.
The last one, ``with(con ...'' turns (temporarily) the data frame ``con''
into a data base on your search path. The components ``rel'' and ``len''
are then objects in this data base and thereby become accessible.
A fifth way to proceed (***NOT*** recommended) is to do:
attach(con)
plot(rel,len)
detach(con)
This is something like the ``with'' solution; with() automates the
attaching and detaching.
The difference is that if you ***have*** got objects ``rel'' and ``len''
in your workspace (and these are different from the columns of ``con'')
then the attach()---detach() procedure will use the objects in your
workspace. They ``mask'' the columns of ``con''. The with() procedure
is not beset with this problem.
HTH
cheers,
Rolf Turner
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
More information about the R-help
mailing list