From hb at biostat.ucsf.edu  Mon Jan 31 05:14:52 2011
From: hb at biostat.ucsf.edu (Henrik Bengtsson)
Date: Sun, 30 Jan 2011 20:14:52 -0800
Subject: [Rd] data.frame(A=X): Prepend "A." also for single-column X:s [WISH]
Message-ID: <AANLkTinbH0vd7z0+CLe4OKKGGp4CmNwugX51URHBcCs2@mail.gmail.com>

Hi.

The following results in a data.frame with column names starting with "A.":

> X <- data.frame(x=1:2, y=1:2)
> X
   x y
1 1 1
2 2 2

> data.frame(A=X)
  A.x A.y
1   1   1
2   2   2


whereas with a single-column matrix you won't get "A.":

> Y <- X[,1,drop=FALSE];
> Y
  x
1 1
2 2

> data.frame(A=Y);
  x
1 1
2 2

I'd like to obtain column name "A.x" in the latter case too.  Note
that Y is still a matrix.

What I am really looking for is a way to automatically prefix data
frame names, e.g:

> data.frame(A=Y, B=Y);  # wish
A.x B.x
1 1   1
2 2   2

analogously to:

> data.frame(A=X, B=X);

  A.x A.y B.x B.y
1   1   1   1   1
2   2   2   2   2

instead of as now:

> data.frame(A=Y, B=Y);
  x x.1
1 1   1
2 2   2


Looking at the code for data.frame(), I find:

        if (ncols[i] > 1L) {
            if (length(namesi) == 0L)
                namesi <- seq_len(ncols[i])
            if (no.vn[i])
                vnames[[i]] <- namesi
            else vnames[[i]] <- paste(vnames[[i]], namesi, sep = ".")
        }
        else {
          ...
        }

I guess that (ncols[i] > 1L) test causes data.frame() to treat the
one-column case specially, and replacing it with (ncols[i] > 0L) would
create "A.x".

Acknowledging that data.frame() has legacy, is there any possibility
for updating this behavior?  Note that the current output is still
obtained if leaving out "A", i.e.

> data.frame(Y);
  x
1 1
2 2

/Henrik



[Rd] data.frame(A=X): Prepend "A." also for single-column X:s [WISH]
Henrik Bengtsson