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

2020-04-07 Thread Ivan Calandra
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

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

2020-04-06 Thread Hervé Pagès
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[

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

2020-04-06 Thread Ivan Calandra
Thank you Greg for the insights! I agree with you that the decrease in speed is not worth the decrease in readability, and I'll change my length() calls to ncol(). Best, Ivan -- Dr. Ivan Calandra TraCEr, laboratory for Traceology and Controlled Experiments MONREPOS Archaeological Research

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

2020-04-03 Thread Greg Snow
As others have pointed out, ncol calls the length function, so you are pretty safe in terms of output of getting the same result when applied to the results of functions like read.csv (there will be a big difference if you ever apply those functions to a matrix or some other data structures). One

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

2020-03-31 Thread William Michels via R-help
Hi Ivan, Like Ivan Krylov, I'm not aware of circumstances for simple dataframes where ncol(DF) does not equal length(DF). As I understand it, using ncol() versus length() is important when you're examining an object returned from a function like sapply(), since sapply() will simplify one-column

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

2020-03-31 Thread Ivan Calandra
Thanks Matthias for the details! 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

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

2020-03-31 Thread Prof. Dr. Matthias Kohl
should have added: dim(x)[2L] -> length(x) Am 31.03.20 um 16:21 schrieb Prof. Dr. Matthias Kohl: Dear Ivan, if I enter ncol in the console, I get function (x) dim(x)[2L] indicating that function dim is called. Function dim has a method for data.frame; see methods("dim"). The dim-method

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

2020-03-31 Thread Prof. Dr. Matthias Kohl
Dear Ivan, if I enter ncol in the console, I get function (x) dim(x)[2L] indicating that function dim is called. Function dim has a method for data.frame; see methods("dim"). The dim-method for data.frame is dim.data.frame function (x) c(.row_names_info(x, 2L), length(x)) Hence, it

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

2020-03-31 Thread Ivan Calandra
Thanks Ivan for the answer. So it confirms my first thought that these two functions are equivalent when applied to a "simple" data.frame. The reason I was asking is because I have gotten used to use length() in my scripts. It works perfectly and I understand it easily. But to be honest, ncol()

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

2020-03-31 Thread Ivan Krylov
On Tue, 31 Mar 2020 14:47:54 +0200 Ivan Calandra wrote: > 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? I am not aware of any exceptions to

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

2020-03-31 Thread Ivan Calandra
That's exactly why I was asking if it really is equivalent and if there are issues using one function or the other -- Dr. Ivan Calandra TraCEr, laboratory for Traceology and Controlled Experiments MONREPOS Archaeological Research Centre and Museum for Human Behavioural Evolution Schloss Monrepos

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

2020-03-31 Thread Eric Berger
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 wrote: > Thanks Eric, > > I know that, but that doesn't really answer my question, does

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

2020-03-31 Thread Ivan Calandra
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

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

2020-03-31 Thread Eric Berger
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 wrote: > Dear useRs, > > I have a very simple question: > On a simple data.frame (i.e. each element is a vector), ncol() and > length()

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

2020-03-31 Thread Ivan Calandra
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