-1

I want to exclude every row from database where gender!="M" and gender!="F" and clean the database.

Thanks, in advance.

This is what I have tried:

CleanGender<-which(data$Gender!="M" & data$Gender!="F")
NelsonGon
  • 13,015
  • 7
  • 27
  • 57
  • 3
    Please add a sample of your data with `dput(head(data,12))` or simply copy and paste the output of `head(data)` to the question. – NelsonGon Jun 06 '19 at 14:15
  • 4
    I think the `&` is the issue. You can't have both elements in the same row You may need `subset(data, gender %in% c("M", "F"))` – akrun Jun 06 '19 at 14:15

2 Answers2

0

We can use %in% to subset multiple values on a column. It would check whether these values are present in the column, create a logical expression and subset those rows

subset(data, gender %in% c("M", "F"))
akrun
  • 874,273
  • 37
  • 540
  • 662
0

Supposing gender can't be both 'M' and 'F' (which look sequentially at vector rows) :

# find index you want to keep
CleanGender_index <- which(data$Gender!="M" | data$Gender!="F")

# select corresponding rows, renaming your data frame is called `df_raw`
df_clean <- df_raw[CleanGender_index, ]

data may not the best name for your dataset since it is also an R function.

cbo
  • 1,664
  • 1
  • 12
  • 27