On 28/10/2023 4:45 p.m., Bert Gunter wrote:
Jeff, et. al. : but ...

Note that as.data.frame() *already* changes the matrix object by adding
column names of *its own choosing* when the matrix has none. So the issue
here is not *whether* col names should be added, but *what*/*how* they
should be. Unless you wish to extend your criticism to the current version
for its failure to adhere to your proscription.

Dataframes have to have column names. The function isn't modifying the object any more than it has to.

Duncan Murdoch


Cheers,
Bert

On Sat, Oct 28, 2023 at 11:55 AM Jeff Newmiller via R-help <
r-help@r-project.org> wrote:

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.ste...@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.gr...@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-boun...@r-project.org> On Behalf Of Boris Steipe
Sent: Saturday, October 28, 2023 1:54 PM
To: R. Mailing List <r-help@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@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@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@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.


        [[alternative HTML version deleted]]

______________________________________________
R-help@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@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.

Reply via email to