Still learning basic syntax in R. Basic example code is provided below.

I know how to call consecutive rows but what if, for example, I wanted the 1st and 3rd row? Or better yet, since that is easily called by w[-2,] in this example, what if the data set was larger and I had the need to investigate the 3rd, 5th and 8th row?

> w<-data.frame(names=c("betty","freddy","sammy"),high.fiving.ability=c(50,50,100))
> w[2:3,]
   names high.fiving.ability
2 freddy                  50
3  sammy                 100

BTW I looked at other posts including this one: Filtering a dataframe but what I am looking for is even more basic than that.

  • $\begingroup$ You should start with some introduction to R! Here you wanna look at the c function. (solution w[c(3,5,8),]) I like Maindonald & Braun (2010): amazon.com/Data-Analysis-Graphics-Using-Example-Based/dp/… $\endgroup$
    – Henrik
    Jun 13, 2012 at 22:49
  • $\begingroup$ This is a perfectly good question, but because it's only about how to get R to do something, & not about any statistical issues (possibly w/ R), it belongs on Stack Overflow, not here. $\endgroup$ Jun 14, 2012 at 1:19

1 Answer 1


You can index using a vector of indices of only the rows you care about. In this case what you're looking for is

w[c(3, 5, 8), ]
  • $\begingroup$ Probably worthwhile to add that you can also put logical conditions here as well. One generic one that I find particularly useful is "list[!(list %in% names(dataframe))]" which gives you all of the values in "list" which are not columns in "dataframe" - saves doing a manual scan, especially if you used capitals for one name and not the other. $\endgroup$ Jun 13, 2012 at 23:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.