[R] problem subsetting data.frame in R version 2.15.2 for Windows

PIKAL Petr petr.pikal at precheza.cz
Thu Mar 21 09:59:18 CET 2013


Hi

in interactive sessions it is convenient way to call data frame columns and repeatedly use them in calculations. 
You just have to be careful with some functions as they can be useful but dangerous.

rm(something) removes an object from environment without warning.

It is usually not a fault of a program, when something gets wrong.

Regards
Petr


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Pierrick Bruneau
> Sent: Thursday, March 21, 2013 9:13 AM
> To: Michael Weylandt
> Cc: r-help at r-project.org
> Subject: Re: [R] problem subsetting data.frame in R version 2.15.2 for
> Windows
> 
> OK, I just had a look at the Good Practice section of ?attach, it
> indeed looks bad...
> 
> Maybe this danger should be emphasized in ?attach, or the function even
> deprecated (why maintaining ugly patterns when better solutions exist?)
> 
> 
> On Thu, Mar 21, 2013 at 8:58 AM, Michael Weylandt <
> michael.weylandt at gmail.com> wrote:
> 
> >
> >
> > On Mar 21, 2013, at 7:39, Pierrick Bruneau <pbruneau at gmail.com>
> wrote:
> >
> > > Hi Borja,
> > >
> > > You may issue:
> > >
> > > attach(data)
> >
> > No -- bad idea -- dangerous -- confusing statefulness, etc. (See
> > explanations in the archives as to why)
> >
> > >
> > > which results in adding your column names to the search path of R
> > > for
> > name
> > > resolving.
> > >
> > > Pierrick Bruneau
> > > CRP Gabriel Lippmann
> > >
> > >
> > > On Wed, Mar 20, 2013 at 11:17 PM, Borja .
> <borjalatorre at outlook.com>
> > wrote:
> > >
> > >> Good day.
> > >> I create a data frame like this:
> > >>> data <- data.frame(a=1:10,b=11:20,c=21:30)
> > >> I can subset this data.frame by saying:
> > >>> data[data$a>7,]
> > >> and I get this result
> > >>                          a  b  c                    8   8 18 28
> > >>         9   9 19 29                  10 10 20 30
> > >> I understand I should get the same result by saying
> > >>> data[a>7,0]
> > >> but I don't. Instead I get:
> > >>                Error in `[.data.frame`(data, a > 7, 0) : object
> 'a'
> > >> not
> >
> > Try instead
> >
> > with(dat, dat[a > 0, ])
> >
> > for a cleaner option.
> >
> > MW
> >
> >
> > >> found
> > >> Thank you very much in advance
> > >>        [[alternative HTML version deleted]]
> > >>
> > >> ______________________________________________
> > >> R-help at r-project.org mailing list
> > >> https://stat.ethz.ch/mailman/listinfo/r-help
> > >> PLEASE do read the posting guide
> > >> http://www.R-project.org/posting-guide.html
> > >> and provide commented, minimal, self-contained, reproducible code.
> > >
> > >    [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > R-help at r-project.org mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> >
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list