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

Rolf Turner r@turner @end|ng |rom @uck|@nd@@c@nz
Tue Feb 5 23:22:24 CET 2019


Consider the following:

set.seed(42)
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)
names(D1)
[1] "a"   "b"   "a:x" "b:x"
names(D2)
[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?

cheers,

Rolf Turner

-- 
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276



More information about the R-help mailing list