[R] col.names in as.data.frame() ?
Boris Steipe
bor|@@@te|pe @end|ng |rom utoronto@c@
Sat Oct 28 20:39:22 CEST 2023
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 using 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 using r-project.org> On Behalf Of Boris Steipe
> Sent: Saturday, October 28, 2023 1:54 PM
> To: R. Mailing List <r-help using 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 using 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.
>
More information about the R-help
mailing list