[R] Inconsistency in 'names' when calling data.frame on other data.frames
Rui Barradas
ru|pb@rr@d@@463 @end|ng |rom gm@||@com
Sat Aug 22 09:06:58 CEST 2026
Hello,
Yes, this is documented. From ?data.frame, section Value
How the names of the data frame are created is complex, and the rest
of this paragraph is only the basic story. [...]
For a named matrix/list/data frame argument with more than one named
column, the names of the columns are the name of the argument followed
by a dot and the column name inside the argument: if the argument is
unnamed, the argument's column names are used.
[...]
You can protect data.frame's arguments with I(). Compare the two calls below.
df1 <- data.frame(a = data.frame(A = 1, C = 1), b = data.frame(B = 1, D = 1))
str(df1)
#> 'data.frame': 1 obs. of 4 variables:
#> $ a.A: num 1
#> $ a.C: num 1
#> $ b.B: num 1
#> $ b.D: num 1
names(df1)
#> [1] "a.A" "a.C" "b.B" "b.D"
df2 <- data.frame(a = I(data.frame(A = 1, C = 1)), b = I(data.frame(B
= 1, D = 1)))
str(df2)
#> 'data.frame': 1 obs. of 2 variables:
#> $ a:Classes 'AsIs' and 'data.frame': 1 obs. of 2 variables:
#> ..$ A: num 1
#> ..$ C: num 1
#> $ b:Classes 'AsIs' and 'data.frame': 1 obs. of 2 variables:
#> ..$ B: num 1
#> ..$ D: num 1
names(df2)
#> [1] "a" "b"
df2 # the names come from the print method
#> a.A a.C b.B b.D
#> 1 1 1 1 1
Hope this helps,
Rui Barradas
Peter Langfelder <peter.langfelder using gmail.com> escreveu (sexta,
21/08/2026 à(s) 17:01):
>
> 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
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list