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://www.researchgate.net/profile/Ivan_Calandra
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 at 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://www.researchgate.net/profile/Ivan_Calandra > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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]]
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://www.researchgate.net/profile/Ivan_Calandra 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 at rgzm.de > <mailto:calandra at 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://www.researchgate.net/profile/Ivan_Calandra > > ______________________________________________ > R-help at r-project.org <mailto:R-help at r-project.org> mailing list -- > To UNSUBSCRIBE and more, see > 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. >
On Tue, 31 Mar 2020 14:47:54 +0200 Ivan Calandra <calandra at rgzm.de> 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 ncol(dataframe)==length(dataframe) (in fact, ncol(x) is dim(x)[2L] and ?dim says that dim(dataframe) returns c(length(attr(dataframe, 'row.names')), length(dataframe))), but watch out for AsIs columns which can have columns of their own: x <- data.frame(I(volcano)) dim(x) # [1] 87 1 length(x) # [1] 1 dim(x[,1]) # [1] 87 61 -- Best regards, Ivan
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() is more intuitive to most users (especially the novice) so I was thinking about switching to using this function instead (all my data.frames are created from read.csv() or similar functions so there should not be any issue). But before doing that, I want to be sure that it is not going to create unexpected results. Thank 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://www.researchgate.net/profile/Ivan_Calandra On 31/03/2020 16:00, Ivan Krylov wrote:> On Tue, 31 Mar 2020 14:47:54 +0200 > Ivan Calandra <calandra at rgzm.de> 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 ncol(dataframe)==length(dataframe) > (in fact, ncol(x) is dim(x)[2L] and ?dim says that dim(dataframe) > returns c(length(attr(dataframe, 'row.names')), length(dataframe))), but > watch out for AsIs columns which can have columns of their own: > > x <- data.frame(I(volcano)) > dim(x) > # [1] 87 1 > length(x) > # [1] 1 > dim(x[,1]) > # [1] 87 61 > >