Skip to content
Prev 395374 / 398502 Next

col.names in as.data.frame() ?

Sent a slightly shorter version of this to your email, this one is to 
the list:
On 28/10/2023 1:54 p.m., Boris Steipe wrote:
> > I have been trying to create a data frame from some structured text 
in a single expression. Reprex:
 > >
 > > nouns <- as.data.frame(
 > >    matrix(c(
 > >      "gaggle",
 > >      "geese",
 > >
 > >      "dule",
 > >      "doves",
 > >
 > >      "wake",
 > >      "vultures"
 > >    ), ncol = 2, byrow = TRUE),
 > >    col.names = c("collective", "category")
 > > )
 > >

You are calling it on a matrix, so the as.data.frame.matrix method is
what matters.  It doesn't have a col.names argument, only row.names.

The docs are vague about what ... does, but if you look at the method,
you can see any unnamed arguments are ignored completely.

If you want to specify the column names in a single call, you'll need to 
put them in the matrix, e.g.

as.data.frame(
   matrix(c(
     "gaggle",
      "geese",

      "dule",
      "doves",

      "wake",
      "vultures"
    ), ncol = 2, byrow = TRUE,
    dimnames =  list(NULL, c("collective", "category"))
   )
)


Duncan Murdoch