[Tagged] Re: col.names in as.data.frame() ?
Jef, your terse reply was so constructive that you converted me! LOL! That is an interesting point though that I remain a bit unclear on. Both data.frame and as.data.frame can be used in some ways similarly as in:
data.frame(matrix(1:12, nrow=3))
X1 X2 X3 X4 1 1 4 7 10 2 2 5 8 11 3 3 6 9 12
as.data.frame(matrix(1:12, nrow=3))
V1 V2 V3 V4 1 1 4 7 10 2 2 5 8 11 3 3 6 9 12 But yes, the constructor accepts many arguments while the converter really normally handles a single object. Where do some other things like cbind() fit, not to mention a dplyr function like tribble()? I do wonder though, why asking a converter to convert a matrix to a data.frame and perhaps adding column names, is considered changing the object contents. The manual page for as.data.frame seems to include quite a few options to specify the name of a single column, truncate column names, deal with row names, and more as well as whether to convert strings to factors. Are those things different enough than what we are discussing? Of course, we may indeed be experiencing mission creep where something simple keeps being improved with new features until the original simplicity and clarity gets lost. -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Jeff Newmiller via R-help Sent: Saturday, October 28, 2023 2:54 PM To: r-help at r-project.org; Boris Steipe <boris.steipe at utoronto.ca>; R. Mailing List <r-help at r-project.org> Subject: Re: [R] [Tagged] Re: col.names in as.data.frame() ? as.data.frame is a _converter_, while data.frame is a _constructor_. Changing the object contents is not what a conversion is for.
On October 28, 2023 11:39:22 AM PDT, Boris Steipe <boris.steipe at utoronto.ca> wrote:
Thanks Duncan and Avi! That you could use NULL in a matrix() dimnames = list(...) argument wasn't clear to me. I thought that would be equivalent to a one-element list - and thereby define rownames. So that's good to know. The documentation could be more explicit - but it is probably more work to do that than just patch the code to honour a col.names argument. (At least I can't see a reason not to.) Thanks again! :-)
On Oct 28, 2023, at 14:24, avi.e.gross at gmail.com wrote:
?????,
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:
nouns
collective 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:
nouns
V1 V2 1 gaggle geese 2 dule doves 3 wake vultures
colnames(nouns)
[1] "V1" "V2"
colnames(nouns) <- c("collective", "category")
nouns
collective 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.
______________________________________________ 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.
Sent from my phone. Please excuse my brevity. ______________________________________________ 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.