From ruipbarradas at sapo.pt  Wed Apr 12 18:07:45 2017
From: ruipbarradas at sapo.pt (Rui Barradas)
Date: Wed, 12 Apr 2017 17:07:45 +0100
Subject: [Rd] "table(droplevels(aq)$Month)" in manual page of droplevels
In-Reply-To: <5d3bcfef-ff20-7d65-e882-7234a87441c7@gmail.com>
References: <58EE202A020000A10002595B@gwsmtp1.uni-regensburg.de>
 <58EE202A020000A10002595B@gwsmtp1.uni-regensburg.de>
 <b66fe849-bb8d-f00d-87e5-553f866d57e0@gmail.com>
 <58EE3C2F020000A10002595F@gwsmtp1.uni-regensburg.de>
 <5d3bcfef-ff20-7d65-e882-7234a87441c7@gmail.com>
Message-ID: <58EE50D1.8060609@sapo.pt>

Hello,

Inline.

Em 12-04-2017 16:40, Henric Winell escreveu:
> (Let's keep the discussion on-list -- I've added back R-devel.)
>
> On 2017-04-12 16:39, Ulrich Windl wrote:
>
>>>>> Henric Winell <nilsson.henric at gmail.com> schrieb am 12.04.2017
>>>>> um 15:35 in
>> Nachricht <b66fe849-bb8d-f00d-87e5-553f866d57e0 at gmail.com>:
>>> On 2017-04-12 14:40, Ulrich Windl wrote:
>>>
>>>> The last line of the example in droplevels' manual page seems to
>>>> be incorrect to me. I think it should read:
>>>> "table(droplevels(aq$Month))". Amazingly (I don't understand)
>>>> both variants seem to produce the same result (R 3.3.3): ---
>>>
>>> The manual says that "The function 'droplevels' is used to drop
>>> unused levels from a 'factor' or, more commonly, from factors in a
>>> data frame." and, as documented, the 'droplevels' generic has
>>> methods for objects of class "data.frame" and "factor".  So, your
>>> being amazed is a bit surprising given that 'aq' is a data frame.
>>
>> The "surprising" thing is the syntax: I was unaware that '$' is a
>> generic operator that can be applied to the result of a function
>> (i.e.: droplevels); I thought it's kind of a special variable syntax.
>
> Then your surprise is unrelated to the use of 'droplevels'.
>
> Since the 'droplevels' method for objects of class "data.frame" returns
> a data frame, the extraction operator '$' works directly on the
> resulting object.  So, 'droplevels(aq)$Month' is essentially the same as
>
> aq <- droplevels(aq)
> aq$Month
>
>  > Isn't there also the syntax ``droplevels(aq)["Month"]''?
>
> Sure, and there are even more ways to do subsetting.  But this is basic
> stuff and therefore off-topic for R-devel.  Please see the manual
> (?Extract) or, e.g., Chapter 3 of Hadley Wickham's "Advanced R".

But note that droplevels(aq)["Month"] and droplevels(aq)$Month are _not_ 
the same. The first returns a data.frame (with just one vector), the 
latter returns a vector. To return just a vector you could also use

droplevels(aq)[["Month"]]

which is preferable for programming, by the way. The '$' operator should 
be reserved for interactive use only.

Hope this helps,

Rui Barradas
>
>
> Henric Winell
>
>
>
>>
>> Regards, Ulrich
>>
>>>
>>>
>>> Henric Winell
>>>
>>>
>>>
>>>>> aq <- transform(airquality, Month = factor(Month, labels =
>>>>> month.abb[5:9])) aq <- subset(aq, Month != "Jul")
>>>>> table(aq$Month)
>>>>
>>>> May Jun Jul Aug Sep 31  30   0  31  30
>>>>> table(droplevels(aq)$Month)
>>>>
>>>> May Jun Aug Sep 31  30  31  30
>>>>> table(droplevels(aq$Month))
>>>>
>>>> May Jun Aug Sep 31  30  31  30
>>>>>
>>>> --- For the sake of learners, try to keep the examples simple
>>>> and useful, even though you experts want to impress the
>>>> newbees...
>>>>
>>>> Ulrich
>>>>
>>>> ______________________________________________
>>>> R-devel at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>>>
>>
>>
>>
>>
>>
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] "table(droplevels(aq)$Month)" in manual page of droplevels
Rui Barradas