[R] data.frame() versus as.data.frame() applied to a matrix.

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Wed Feb 6 00:27:26 CET 2019

I have no idea about "why it is this way" but there are many cases where I would rather have to use backticks around syntactically-invalid names than deal with arbitrary rules for mapping column names as they were supplied to column names as R wants them to be. From that perspective, making the conversion function leave the names alone and limit the name-mashing to one function sounds great to me. You can always call make.names yourself.

On February 5, 2019 2:22:24 PM PST, Rolf Turner <r.turner using auckland.ac.nz> wrote:
>Consider the following:
>X <- matrix(runif(40),10,4)
>colnames(X) <- c("a","b","a:x","b:x") # Imitating the output
>                                       # of model.matrix().
>D1 <- as.data.frame(X)
>D2 <- data.frame(X)
>[1] "a"   "b"   "a:x" "b:x"
>[1] "a"   "b"   "a.x" "b.x"
>The names of D2 are syntactically valid; those of D1 are not.
>Why should I have expected this phenomenon? :-)
>The as.data.frame() syntax seems to me much more natural for converting
>a matrix to a data frame, yet it doesn't get it quite right, sometimes,
>in respect of the names.
>Is there some reason that as.data.frame() does not apply make.names()?
>Or was this just an oversight?
>Rolf Turner

Sent from my phone. Please excuse my brevity.

More information about the R-help mailing list