[R] Access Rows in a Data Frame by Row Name

Berton Gunter gunter.berton at gene.com
Wed Sep 13 21:11:39 CEST 2006


The answer is yes, you can access rows of a data.frame by rowname in the
same way as columns, which you could have found by merely trying it. Don't
overlook the value of a little experimentation as the fastest way to an
answer.

-- Bert Gunter
Genentech
 

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Tony Plate
> Sent: Wednesday, September 13, 2006 11:02 AM
> To: Michael Gormley
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] Access Rows in a Data Frame by Row Name
> 
> Matrix-style indexing works for both columns and rows of data frames.
> 
> E.g.:
>  > x <- data.frame(a=1:5, b=6:10, d=11:15)
>  > x
>    a  b  d
> 1 1  6 11
> 2 2  7 12
> 3 3  8 13
> 4 4  9 14
> 5 5 10 15
>  > x[2:4,c(1,3)]
>    a  d
> 2 2 12
> 3 3 13
> 4 4 14
>  >
> 
> Time spend reading the help document "An Introduction to R" will 
> probably be well worth it.  The relevant sections are "5 Arrays and 
> matrices", and "6.3 Data frames".
> 
> -- Tony Plate
> 
> Michael Gormley wrote:
> > I have created a data frame using the read.table command.  
> I want to be able to access the rows by the row name, or a 
> vector of row names. I know that you can access columns by 
> using the data.frame.name$col.name.  Is there a way to access 
> row names in a similar manner?
> > 
> > 	[[alternative HTML version deleted]]
> > 
> > ______________________________________________
> > R-help at stat.math.ethz.ch 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.
> >
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>



More information about the R-help mailing list