Skip to content
Prev 46279 / 63461 Next

cbind error with check.names

One rationale is that data.frame(check.names=TRUE,...) does two things: it makes
sure there are no duplicate names and it makes sure that all the names are syntactic
names.  If you have created a data.frame with non-syntactic names you would be annoyed
if a call to cbind mangled its names, hence cbind.data.frame sets check.names=FALSE
to avoid this.
  > d1 <- data.frame(`Mass (g)`=102, `Conc (% by vol.)`=0.23, check.names=FALSE)
  > d2 <- data.frame(`Accel (m/s/s)`=9.81, `Conc (% by vol.)`=0.23, check.names=FALSE)
  > names(d1)
  [1] "Mass (g)"         "Conc (% by vol.)"
  > names(d2)
  [1] "Accel (m/s/s)"    "Conc (% by vol.)"
  > names(data.frame(d1, d2, check.names=TRUE))
  [1] "Mass..g."           "Conc....by.vol.."   "Accel..m.s.s."      "Conc....by.vol...1"
  > names(data.frame(d1, d2, check.names=FALSE))
  [1] "Mass (g)"         "Conc (% by vol.)" "Accel (m/s/s)"    "Conc (% by vol.)"
  > names(cbind(d1, d2))
  [1] "Mass (g)"         "Conc (% by vol.)" "Accel (m/s/s)"    "Conc (% by vol.)"

Perhaps data.frame() should throw an error if there are duplicate names,
or perhaps it should have a separate argument to say what to do about duplicate names,
but changing that sort of thing now would break a fair bit of code.  Perhaps
cbind.data.frame should not call data.frame, but copy the work that data.frame
does or perhaps it should check for duplicate names on the output of data.frame().
Is it worth the time to do that?

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com