[R] Inconsistency in 'names' when calling data.frame on other data.frames

Peter Langfelder peter@|@ng|e|der @end|ng |rom gm@||@com
Sat Aug 22 16:41:51 CEST 2026


Thanks all for pointing me to the help which I missed and it indeed
explains it. It would be useful for my work if the data.frame function
contained an additional argument allowing the user to force or disable
the addition of the argument name. As a suggestion of what that might
look like, I modified the dataframe.R source file in base package in
that I added an argument composite.names with default value NULL which
retains the current behavior; the user could also specify FALSE and
TRUE to disable/enable the addition of the prefix irrespective of the
number of columns in each argument. The diff result is below. Is that
something the R core team would consider? (No big deal if not, I can
always use my version of the data.frame function if needed, and
apologies if this should be moved to the R-devel list...)

The diff follows.

diff dataframe.R dataframe.PL.R
424a425
>              composite.names = NULL,
478a480,484
>     if (!is.null(composite.names)) {
>       if (!is.logical(composite.names)) stop("'composite.names' must be logical.");
>       if (is.na(composite.names)) composite.names <- NULL;
>     }
>     doCompositeNames <- if (is.null(composite.names)) -1L else as.integer(0 + composite.names);
489c495
< if(ncols[i] > 1L) {
---
> if ((ncols[i] > 1L && doCompositeNames!= 0L) || doCompositeNames==1) {

Peter

On Sat, Aug 22, 2026 at 7:23 PM Gabor Grothendieck
<ggrothendieck using gmail.com> wrote:
>
> That does seem odd although I gather from the other answers that it is intended.
> Note that we can avoid this by simply not naming the arguments in the
> first place.
>
> > data.frame(data.frame(A = 1), data.frame(B = 1))
>   A B
> 1 1 1
> > data.frame(data.frame(A = 1, C = 1), data.frame(B = 1, D = 1))
>   A C B D
> 1 1 1 1 1
>
> On Fri, Aug 21, 2026 at 12:01 PM Peter Langfelder
> <peter.langfelder using gmail.com> wrote:
> >
> > Hi all,
> >
> > when calling data.frame on other data frames and naming arguments, the
> > way the 'names' of the result are formed seems to depend on whether
> > the argument data frames have one or more columns. Specifically,
> > consider this:
> >
> > > data.frame(a = data.frame(A = 1), b = data.frame(B = 1))
> >   A B
> > 1 1 1
> >
> >
> > Here the column names are simply copied from column names of the arguments.
> > In contrast, if the argument data frames have two (or more) columns,
> > here's what happens:
> >
> > > data.frame(a = data.frame(A = 1, C = 1), b = data.frame(B = 1, D = 1))
> >   a.A a.C b.B b.D
> > 1   1   1   1   1
> >
> >
> > The 'names' of the result are now <name of the argument>.<column name
> > of the argument value>
> >
> > Is this behavior intended?
> >
> > Thanks,
> >
> > Peter
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com



More information about the R-help mailing list