[R] no doubt a dumb question, but..

Marc Schwartz MSchwartz at MedAnalytics.com
Tue Nov 9 05:27:47 CET 2004


On Mon, 2004-11-08 at 21:56, Dr. John R. Vokey wrote:
> Yes, I am a newbie at R, but it is not the complex commands in R that 
> have me baffled, but simple data commands.  For example, why does 
> something like:
> 
>  > plot(Girth ~ Height)
> 
> *not* work after a command that allegedly loads the data:
> 
>  > data(trees)
> 
> with the error message:
> 
> Error in eval(expr, envir, enclos) : Object "Girth" not found
> 
> but does work after the command:
> 
>  > attach(trees)
> 
> ?

As per ?attach:

"By attaching a data frame to the search path it is possible to refer to
the variables in the data frame by their names alone, rather than as
components of the data frame (eg in the example below, height rather
than women$height)."

data(trees) simply loads the dataset, but does not place it in the
search path, which is what attach(trees) does.

Prior to using attach(trees), you would need to use:

plot(trees$Girth ~ trees$Height)

to tell R that the variables Girth and Height are in the data frame
called 'trees'.

HTH,

Marc Schwartz




More information about the R-help mailing list