I have quite a large dataset. I would like to exclude people who have certain conditions.
In other words, I want to drop rows that have answered yes to the question.
How do I do that?
Thank you very much.
I have quite a large dataset. I would like to exclude people who have certain conditions.
In other words, I want to drop rows that have answered yes to the question.
How do I do that?
Thank you very much.
We can use rowSums
on a logical matrix
. If the whole dataset includes columns with 'yes', 'no' values, convert the dataset to a logical matrix df1 == 'yes'
, get the rowSums
, check if it is equal to 0 i.e. the row doesn't haven't any 'yes', use that to subset the rows
df1[rowSums(df1 == 'yes', na.rm = TRUE) == 0,]
If it is a single column, use subset
subset(df1, col1 == "yes")
I have done it using data.table
pakage in R,
If you have a data with the column "yes" and "no" and wanted to remove those rows with "yes", you can try this
df <- data.table(A=seq(1:5),B=c("yes","yes","no","no","no"))
df
A B
1: 1 yes
2: 2 yes
3: 3 no
4: 4 no
5: 5 no
df[!B=="yes",]
A B
1: 3 no
2: 4 no
3: 5 no
Hope it will serve your purpose.