Jojo
Jojo

Reputation: 5201

Return all rows of a data frame with a certain value

I have a data frame with multiple columns, one of which (called: drift.N) is a series of TRUE's and FALSES's. How would I go about separating the "TRUE" rows from the "FALSE" rows or asking R to tell me which rows drift.N=="TRUE" ?

Upvotes: 0

Views: 17347

Answers (2)

Joshua
Joshua

Reputation: 688

It is really quite easy because R can use logical indexing. So if drift.N already contains TRUE/FALSE, then simply:

yourdata[yourdata[, "drift.N"], ]

should work. Basically, pass the column vector yourdata[, "drift.N"] as the row subset you want from your whole data frame, yourdata. The rows where drift.N == TRUE will be returned.

Upvotes: 2

Paul Hiemstra
Paul Hiemstra

Reputation: 60944

If you have a data.frame called df:

df[df$column_name,]

gets you the subset of the data.frame where column_name equals TRUE. To get the FALSE subset:

df[!df$column_name,]

(spot the exclamation mark !), where ! is NOT. To get the indices where column_name is TRUE:

which(df$column_name)
which(!df$column_name)

Finally, I recommend you go online and download some basic R tutorials and work through them. This questions, and many other basics, will be treated in them. See e.g.:

Upvotes: 11

Related Questions