Home > Enterprise >  Removing 'FALSE' and 'NAs' from a dataframe
Removing 'FALSE' and 'NAs' from a dataframe

Time:10-04

I want to remove 'FALSE' and 'NAs' from a large dataframe. My input looks like,

ID Codes
1 TRUE
2 NA
3 FALSE
4 TRUE

My required output is,

ID Codes
1 TRUE
4 TRUE

Please suggest the best way to do it in R Thanks

CodePudding user response:

We can just use subset and specify Codes (assuming that it is logical column, the NA will be dropped)

subset(df1, Codes)
  ID Codes
1  1  TRUE
4  4  TRUE

data

df1 <- structure(list(ID = 1:4, Codes = c(TRUE, NA, FALSE, TRUE)), 
class = "data.frame", row.names = c(NA, 
-4L))

CodePudding user response:

If your Codes are of type character:

library(dplyr)
df %>%
  filter(Codes == "TRUE")

If they are logical:

df %>%
  filter(Codes)

CodePudding user response:

Here is another base R approach: Using complete.cases

df1$Codes[df1$Codes == FALSE] <- NA
df1[complete.cases(df1),]

output:

  ID Codes
1  1  TRUE
4  4  TRUE
  • Related