[Rd] Column name expansion in data.frame()

Michael Hahsler mhahsler at lyle.smu.edu
Thu Aug 27 15:57:42 CEST 2015


Dear R developers:

I am trying to add a column to a data.frame. The following does the 
trick by expanding the name of the first data frame with the prefix foo:

 > data.frame(foo = as.list(data.frame(items = 1:3, bar=1:3)), items = 
1:3)  foo.items foo.bar items
1         1       1     1
2         2       2     2
3         3       3     3

However, the following special case produces an unexpected (at least to 
me) result:
 > data.frame(foo = as.list(data.frame(items = 1:3)), items = 1:3)
   items items.1
1     1       1
2     2       2
3     3       3

I also tried cbind in various ways. This works:
 > cbind(foo = data.frame(items = 1:3, bar=1:3), items = 1:3)
   foo.items foo.bar items
1         1       1     1
2         2       2     2
3         3       3     3


The special case gives again different result with two columns having 
seemingly the same name.
 > cbind(foo = data.frame(items = 1:3), items = 1:3)
   items items
1     1     1
2     2     2
3     3     3
 > cbind(foo = data.frame(items = 1:3), data.frame(items = 1:3))
   items items
1     1     1
2     2     2
3     3     3

Any help would be appreciated.

Thanks,
Michael


sessionInfo:

 > sessionInfo()
R Under development (unstable) (2015-08-23 r69165)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 15.04

locale:
  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
  [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base





-- 
   Michael Hahsler, Assistant Professor
   Department of Engineering Management, Information, and Systems
   Department of Computer Science and Engineering (by courtesy)
   Bobby B. Lyle School of Engineering
   Southern Methodist University, Dallas, Texas

   office: Caruth Hall, suite 337, room 311
   email:  mhahsler at lyle.smu.edu
   web:    http://lyle.smu.edu/~mhahsler



More information about the R-devel mailing list