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) ? - JRV -- There are 10 kinds of people: those who understand binary, and those who don't
no doubt a dumb question, but..
5 messages · Dr. John R. Vokey, Marc Schwartz, Martin Maechler +1 more
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
"Marc" == Marc Schwartz <MSchwartz at MedAnalytics.com>
on Mon, 08 Nov 2004 22:27:47 -0600 writes:
Marc> 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)
>>
>> ?
Marc> As per ?attach:
Marc> "By attaching a data frame to the search path it is
Marc> possible to refer to the variables in the data frame
Marc> by their names alone, rather than as components of the
Marc> data frame (eg in the example below, height rather
Marc> than women$height)."
Marc> data(trees) simply loads the dataset, but does not
Marc> place it in the search path, which is what
Marc> attach(trees) does.
Marc> Prior to using attach(trees), you would need to use:
Marc> plot(trees$Girth ~ trees$Height)
Marc> to tell R that the variables Girth and Height are in
Marc> the data frame called 'trees'.
Yes, thank you, Marc!
For the specific case at hand, however, the recommdend way is to use
plot(Girth ~ Height, data = trees)
Further note that many of us try to avoid attach()ing data frames
(most of the time; not always) and we have provided the nice alternative
with( <data> , <expression_body> )
So, for the current example, you could also say
with(trees, plot(Girth ~ Height))
The main advantage of with(): Only inside it, the components of
'trees' are visible - no need to remember to detach() ;
see also help(with) and its examples.
Martin Maechler
On Tue, 2004-11-09 at 02:07, Martin Maechler wrote:
<SNIP>
Further note that many of us try to avoid attach()ing data frames
(most of the time; not always) and we have provided the nice alternative
with( <data> , <expression_body> )
So, for the current example, you could also say
with(trees, plot(Girth ~ Height))
The main advantage of with(): Only inside it, the components of
'trees' are visible - no need to remember to detach() ;
see also help(with) and its examples.
Right. Thanks Martin. After sending my reply, I kicked myself a few times for not mentioning with()...especially because it also enables a standardized mechanism for accessing data frame variables across functions (not all of which have a 'data = ' argument). Also, there is a substantial savings with respect to overhead by not attaching and detaching (manipulating the search path), which I had noted and benchmarked previously in a post, earlier this year I believe, that I cannot locate at the moment. This can become very important if one is looping (in some fashion) over data frames or subsets of data frames and therefore avoids the repeated calls to attach() and detach(). Best regards, Marc
On Monday 08 November 2004 19: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)
I have wondered this myself, but one reason may be that more than one data table can be loaded at a time. Quite often data tables will use the same names for fields. It would inconvenient for instance, if you had two tables loaded, say one for trees and one for melons with a field named "Girth", and tried a plot(Girth ~ Height) command. So data() more or less accesses that table, and attach explicit identifies the table of interest. JWDougherty