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") ) But ... :> str(nouns)'data.frame': 3 obs. of 2 variables: $ V1: chr "gaggle" "dule" "wake" $ V2: chr "geese" "doves" "vultures" i.e. the col.names argument does nothing. From my reading of ?as.data.frame, my example should have worked. I know how to get the required result with colnames(), but I would like to understand why the idiom as written didn't work, and how I could have known that from the help file. Thanks! Boris
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
@vi@e@gross m@iii@g oii gm@ii@com
2023-Oct-28 18:24 UTC
[R] col.names in as.data.frame() ?
?????, Try this where you tell matrix the column names you want: nouns <- as.data.frame( matrix(c( "gaggle", "geese", "dule", "doves", "wake", "vultures" ), ncol = 2, byrow = TRUE, dimnames=list(NULL, c("collective", "category")))) Result:> nounscollective category 1 gaggle geese 2 dule doves 3 wake vultures The above simply names the columns earlier when creating the matrix. There are other ways and the way you tried LOOKS like it should work but fails for me with a message about it weirdly expecting three rows versus two which seems to confuse rows and columns. My version of R is recent and I wonder if there is a bug here. Consider whether you really need the data.frame created in a single statement or can you change the column names next as in:> nounsV1 V2 1 gaggle geese 2 dule doves 3 wake vultures> colnames(nouns)[1] "V1" "V2"> colnames(nouns) <- c("collective", "category") > nounscollective category 1 gaggle geese 2 dule doves 3 wake vultures Is there a known bug here or is the documentation wrong? -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Boris Steipe Sent: Saturday, October 28, 2023 1:54 PM To: R. Mailing List <r-help at r-project.org> Subject: [R] col.names in as.data.frame() ? 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") ) But ... :> str(nouns)'data.frame': 3 obs. of 2 variables: $ V1: chr "gaggle" "dule" "wake" $ V2: chr "geese" "doves" "vultures" i.e. the col.names argument does nothing. From my reading of ?as.data.frame, my example should have worked. I know how to get the required result with colnames(), but I would like to understand why the idiom as written didn't work, and how I could have known that from the help file. Thanks! Boris ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.