Bonnie
Bonnie

Reputation: 481

subset a certain part of my data.frame

Suppose I have a data.frame that looks something like this:

ball1  ball2  ball3  allRed 
 red    red   blue     F
 red    red    red     T
 blue   blue   red     F
  .      .      .      .
  .      .      .      .
  .      .      .      .

And so on. I wish to extract the first 5 rows of the data frame in which allRed are TRUE, and the first 5 rows of the data.frame in which allRed are FALSE. I stored the data.frame in a variable ball. Below is what I had tried to little avail:

ball[,1][ball$allRed==F]

Upvotes: 0

Views: 61

Answers (2)

Sven Hohenstein
Sven Hohenstein

Reputation: 81743

head(ball[ball$allRed, ], 5)

will do the trick.

Upvotes: 6

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193687

You should subset allRed first, and then 5 rows from that. Also, pay attention to your commas. Try:

ball[ball$allRed == TRUE, ][1:5, ]

Upvotes: 1

Related Questions