简体   繁体   中英

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" ?

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 eg:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM