[R] ncol() vs. length() on data.frames

Ivan Calandra c@|@ndr@ @end|ng |rom rgzm@de
Tue Apr 7 08:46:42 CEST 2020


Dear Hervé,

This is indeed a wise recommendation; I hadn't thought about colnames()
vs. names(), and in general 2D vs. list notations.
I will have to edit a bit more than I thought.

Thank you all for all these hints!

Best,
Ivan

--
Dr. Ivan Calandra
TraCEr, laboratory for Traceology and Controlled Experiments
MONREPOS Archaeological Research Centre and
Museum for Human Behavioural Evolution
Schloss Monrepos
56567 Neuwied, Germany
+49 (0) 2631 9772-243
https://www.researchgate.net/profile/Ivan_Calandra

On 07/04/2020 02:56, Hervé Pagès wrote:
> Hi Ivan,
>
> On 3/31/20 06:44, Ivan Calandra wrote:
>> That's exactly why I was asking if it really is equivalent and if there
>> are issues using one function or the other
>
> Not that I know. It's mostly a matter of taste and code readability.
>
> Either use the 2D interface:
>
>    ncol(df), colnames(df), df[ , "somecol"], cbind(), etc...
>
> or the list interface:
>
>    length(df), names(df), df[["somecol"]], c(), etc...
>
> to operate on your data.frames. They're equivalent. One advantage of
> using the latter though is that your code would also work on list
> objects that are not data.frames. But maybe you don't need or care
> about that in which case using one interface or the other makes no
> difference.
>
> Note that the 2D interface is richer because it has nrow(),
> rownames(), rbind() that are not part of the list interface.
>
> From a code readability point of view I think one should be consistent
> and avoid mixing the 2 styles. For example IMO using length(df) and
> colnames(df) in the same function body is not good style. Either use
> length(df) and names(df), or use ncol(df) and colnames(df). If in the
> same function body you need to also access the rownames() then it
> would make sense to stick to the 2D interface throughout the entire
> body of your function.
>
> Cheers,
> H.
>
>>
>> -- 
>> Dr. Ivan Calandra
>> TraCEr, laboratory for Traceology and Controlled Experiments
>> MONREPOS Archaeological Research Centre and
>> Museum for Human Behavioural Evolution
>> Schloss Monrepos
>> 56567 Neuwied, Germany
>> +49 (0) 2631 9772-243
>> https://urldefense.proofpoint.com/v2/url?u=https-3A__www.researchgate.net_profile_Ivan-5FCalandra&d=DwIDaQ&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=yZXfRqVdJeNf73bgLhzIctzOWowNX-0ccnNHdReiprw&s=rSFaISqU4VRbZBhLtnKvwJmxwkKQ0kAN0sIAfSBWUkw&e=
>>
>>
>> On 31/03/2020 15:39, Eric Berger wrote:
>>> Yes it does because length(list) gives you the number of elements of
>>> the list. And in the case of a data frame object that is the number of
>>> columns, or ncol().
>>>
>>> On Tue, Mar 31, 2020 at 4:37 PM Ivan Calandra <calandra using rgzm.de
>>> <mailto:calandra using rgzm.de>> wrote:
>>>
>>>      Thanks Eric,
>>>
>>>      I know that, but that doesn't really answer my question, does it?
>>>
>>>      Ivan
>>>
>>>      --
>>>      Dr. Ivan Calandra
>>>      TraCEr, laboratory for Traceology and Controlled Experiments
>>>      MONREPOS Archaeological Research Centre and
>>>      Museum for Human Behavioural Evolution
>>>      Schloss Monrepos
>>>      56567 Neuwied, Germany
>>>      +49 (0) 2631 9772-243
>>>     
>>> https://urldefense.proofpoint.com/v2/url?u=https-3A__www.researchgate.net_profile_Ivan-5FCalandra&d=DwIDaQ&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=yZXfRqVdJeNf73bgLhzIctzOWowNX-0ccnNHdReiprw&s=rSFaISqU4VRbZBhLtnKvwJmxwkKQ0kAN0sIAfSBWUkw&e=
>>>
>>>      On 31/03/2020 15:26, Eric Berger wrote:
>>>      > A data frame is a special case of a list. It is a list of its
>>>      columns.
>>>      >
>>>      > > is.list( your_data_frame )
>>>      >
>>>      > # TRUE
>>>      >
>>>      >
>>>      > On Tue, Mar 31, 2020 at 4:04 PM Ivan Calandra <calandra using rgzm.de
>>>      <mailto:calandra using rgzm.de>
>>>      > <mailto:calandra using rgzm.de <mailto:calandra using rgzm.de>>> wrote:
>>>      >
>>>      >     Dear useRs,
>>>      >
>>>      >     I have a very simple question:
>>>      >     On a simple data.frame (i.e. each element is a vector),
>>>      ncol() and
>>>      >     length() will give the same result.
>>>      >
>>>      >     Are they just equivalent on such objects, or are they
>>>      differences in
>>>      >     some cases?
>>>      >     Is one of them to be preferred for whatever reason?
>>>      >
>>>      >     Thanks you,
>>>      >     Ivan
>>>      >
>>>      >     --
>>>      >     Dr. Ivan Calandra
>>>      >     TraCEr, laboratory for Traceology and Controlled Experiments
>>>      >     MONREPOS Archaeological Research Centre and
>>>      >     Museum for Human Behavioural Evolution
>>>      >     Schloss Monrepos
>>>      >     56567 Neuwied, Germany
>>>      >     +49 (0) 2631 9772-243
>>>      >   
>>>  https://urldefense.proofpoint.com/v2/url?u=https-3A__www.researchgate.net_profile_Ivan-5FCalandra&d=DwIDaQ&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=yZXfRqVdJeNf73bgLhzIctzOWowNX-0ccnNHdReiprw&s=rSFaISqU4VRbZBhLtnKvwJmxwkKQ0kAN0sIAfSBWUkw&e=
>>>      >
>>>      >     ______________________________________________
>>>      >     R-help using r-project.org <mailto:R-help using r-project.org>
>>>      <mailto:R-help using r-project.org <mailto:R-help using r-project.org>>
>>>      mailing list --
>>>      >     To UNSUBSCRIBE and more, see
>>>      >   
>>>  https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwIDaQ&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=yZXfRqVdJeNf73bgLhzIctzOWowNX-0ccnNHdReiprw&s=RzvpfqevFgNjYNRLX0nYF8un-5x7k-QG5h0465YA7mQ&e=
>>>      >     PLEASE do read the posting guide
>>>      >   
>>>  https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwIDaQ&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=yZXfRqVdJeNf73bgLhzIctzOWowNX-0ccnNHdReiprw&s=kL6q9dyo4ScG-oZCxbTTWwVV389KO1pOpXMWZWd15bs&e=
>>>      >     and provide commented, minimal, self-contained, reproducible
>>>      code.
>>>      >
>>>
>>>      ______________________________________________
>>>      R-help using r-project.org <mailto:R-help using r-project.org> mailing list --
>>>      To UNSUBSCRIBE and more, see
>>>     
>>> https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwIDaQ&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=yZXfRqVdJeNf73bgLhzIctzOWowNX-0ccnNHdReiprw&s=RzvpfqevFgNjYNRLX0nYF8un-5x7k-QG5h0465YA7mQ&e=
>>>      PLEASE do read the posting guide
>>>     
>>> https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwIDaQ&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=yZXfRqVdJeNf73bgLhzIctzOWowNX-0ccnNHdReiprw&s=kL6q9dyo4ScG-oZCxbTTWwVV389KO1pOpXMWZWd15bs&e=
>>>      and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwIDaQ&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=yZXfRqVdJeNf73bgLhzIctzOWowNX-0ccnNHdReiprw&s=RzvpfqevFgNjYNRLX0nYF8un-5x7k-QG5h0465YA7mQ&e=
>>
>> PLEASE do read the posting guide
>> https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwIDaQ&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=yZXfRqVdJeNf73bgLhzIctzOWowNX-0ccnNHdReiprw&s=kL6q9dyo4ScG-oZCxbTTWwVV389KO1pOpXMWZWd15bs&e=
>> and provide commented, minimal, self-contained, reproducible code.
>>
>



More information about the R-help mailing list