How to access a column by its label?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to access a column by its label?

Neil McLeod
Hi all,

Is there any way to access a column of a data frame by its label (title)
rather than its column index? For example, I'd like to be able to select
animals[,"weight"] rather than animals[,3], if the third column of the
"animals" data frame has the label "weight".

Thank you!

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
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 | Threaded
Open this post in threaded view
|

Re: How to access a column by its label?

vincent-18
?colnames
hih

______________________________________________
[hidden email] mailing list
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 | Threaded
Open this post in threaded view
|

Re: How to access a column by its label?

Marc Schwartz (via MN)
In reply to this post by Neil McLeod
On Thu, 2006-08-03 at 14:44 -0400, Neil McLeod wrote:
> Hi all,
>
> Is there any way to access a column of a data frame by its label (title)
> rather than its column index? For example, I'd like to be able to select
> animals[,"weight"] rather than animals[,3], if the third column of the
> "animals" data frame has the label "weight".
>
> Thank you!

You answered your own question...animals[,"weight"]

You can also do:

animals$weight

or

animals[["weight"]]

or

subset(animals, select = weight)


See ?Extract and ?subset for more information.

HTH,

Marc Schwartz

______________________________________________
[hidden email] mailing list
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.