TJ Wu
TJ Wu

Reputation: 749

keep that row if it contains any "TRUE"

I have a table look like this one

    C1      C2      C3      C4      C5....    
R1  FALSE   FALSE   TRUE    TRUE
R2  FALSE   FALSE   NA      TRUE
R3  NA      NA      NA      TRUE
R4  NA      FALSE   FALSE   FALSE
R5  NA      NA      NA      NA
.
.
.

I want to keep all rows which contain at least one TRUE. In this table, R1, R2 and R3 need to be kept. Then, I can extract another column(C21)'s value from this same table.

Please give me some advise, thank you!

Upvotes: 3

Views: 9837

Answers (3)

GWD
GWD

Reputation: 1464

apply(X = df1, 1, any) 

will give you a logical vector, that you can then use accordingly

i.e. df1[which(apply(df1, 1, any)), ]

Upvotes: 3

Peter
Peter

Reputation: 7790

# Example
x <- 
  matrix(c(FALSE, FALSE, NA, NA, NA, FALSE, FALSE, NA, FALSE, NA, TRUE, NA, NA, FALSE, TRUE, TRUE, FALSE, NA), 
         nrow = 5, ncol = 4, dimnames = list(paste0("R", 1:5), paste0("C", 1:4)))
x
#       C1    C2    C3    C4
# R1 FALSE FALSE  TRUE  TRUE
# R2 FALSE FALSE    NA FALSE
# R3    NA    NA    NA    NA
# R4    NA FALSE FALSE FALSE
# R5    NA    NA  TRUE FALSE

# apply the 'any()' function to the rows, this will return true if there is at
# least one TRUE in the row

apply(x, 1, any)
# R1   R2   R3   R4   R5 
# TRUE   NA   NA   NA TRUE 

# use 'which' to get the row index

which(apply(x, 1, any)) 
# R1 R5 
#  1  5 

# subset the matrix
idx <- which(apply(x, 1, any))

x[idx, ]
#       C1    C2   C3    C4
# R1 FALSE FALSE TRUE  TRUE
# R5    NA    NA TRUE FALSE

Upvotes: 8

akrun
akrun

Reputation: 887098

We can use rowSums on the logical matrix (df1 & !is.na(df1)), check if the sum is greater than 0, use that logical vector to subset the rows.

Subdf <- df1[rowSums(df1 & !is.na(df1)) >0,]
Subdf
#      C1    C2   C3   C4
#R1 FALSE FALSE TRUE TRUE
#R2 FALSE FALSE   NA TRUE
#R3    NA    NA   NA TRUE

Or we can use the na.rm=TRUE in rowSums

df1[rowSums(df1, na.rm=TRUE) > 0,]

We can extract the 'C21' column by Subdf$C21 or Subdf[['C21']] (if the initial dataset is data.frame) or Subdf[, 'C21'] for matrix (in the example, I didn't have 21 columns)

Upvotes: 1

Related Questions