Quantcast

"table(droplevels(aq)$Month)" in manual page of droplevels

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

"table(droplevels(aq)$Month)" in manual page of droplevels

Ulrich Windl
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):
---
> 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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: "table(droplevels(aq)$Month)" in manual page of droplevels

Henric Winell
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.


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
>
> ______________________________________________ [hidden email]
> mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: "table(droplevels(aq)$Month)" in manual page of droplevels

Henric Winell
(Let's keep the discussion on-list -- I've added back R-devel.)

On 2017-04-12 16:39, Ulrich Windl wrote:

>>>> Henric Winell <[hidden email]> schrieb am 12.04.2017
>>>> um 15:35 in
> Nachricht <[hidden email]>:
>> 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".


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
>>>
>>> ______________________________________________
>>> [hidden email] mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>>
>
>
>
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: "table(droplevels(aq)$Month)" in manual page of droplevels

Rui Barradas
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 <[hidden email]> schrieb am 12.04.2017
>>>>> um 15:35 in
>> Nachricht <[hidden email]>:
>>> 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
>>>>
>>>> ______________________________________________
>>>> [hidden email] mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>>>
>>
>>
>>
>>
>>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: "table(droplevels(aq)$Month)" in manual page of droplevels

Martin Maechler
>>>>> Rui Barradas <[hidden email]>
>>>>>     on Wed, 12 Apr 2017 17:07:45 +0100 writes:

    > 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 <[hidden email]> schrieb am
    >> 12.04.2017
>>>>> um 15:35 in
    >>> Nachricht
    >>> <[hidden email]>:
    >>>> 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,

Indeed, we hope..  Thanks to the helpers!

Ulrich, please note that in the end this was all  because you're
still learning to understand R (e.g., data frames !) better.

As such this was completely inappropriate for R-devel and should
have gotten to the R help list  R-help.

With regards,
Martin Maechler, ETH Zurich

    > 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
    >>>>>
    >>>>> ______________________________________________
    >>>>> [hidden email] mailing list
    >>>>> https://stat.ethz.ch/mailman/listinfo/r-devel
    >>>>>
    >>>
    >>>
    >>>
    >>>
    >>>
    >>
    >> ______________________________________________
    >> [hidden email] mailing list
    >> https://stat.ethz.ch/mailman/listinfo/r-devel

    > ______________________________________________
    > [hidden email] mailing list
    > https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Antw: Re: "table(droplevels(aq)$Month)" in manual page of droplevels

Ulrich Windl
>>> Martin Maechler <[hidden email]> schrieb am 13.04.2017 um 16:16 in
Nachricht <[hidden email]>:
[...]
> Ulrich, please note that in the end this was all  because you're
> still learning to understand R (e.g., data frames !) better.

Hi!

I understood that I did not understand the example due to my lack of R knowledge. That is the point you made.
However I read a book on R, the introduction on R, and several tutorials on R, and I still never saw the use pattern presented in the example.
Thus I think my proposal to present more comprehensible examples is a valid one. Alternatively the introduction to R could be extended to a level that allows the user to understand the example presented.
Honestly there are many example I don't understand (plus descriptions that I hope to understand from the examples).
While I understand that R developers prefer to be left alone, not hearing about such issues, I think even R developers could benefit from hearing of beginner's problems.
Anyway R developers are free to forward messages to R-help if they think more appropriate for R-help.

>
> As such this was completely inappropriate for R-devel and should
> have gotten to the R help list  R-help.

Regards,
Ulrich


>
> With regards,
> Martin Maechler, ETH Zurich
[...]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Loading...