[Rd] data.frame(A=X): Prepend "A." also for single-column X:s [WISH]
Henrik Bengtsson
hb at biostat.ucsf.edu
Mon Jan 31 05:14:52 CET 2011
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
More information about the R-devel
mailing list