I have this data frame:
df <- data.frame (ID = c(1:20),
Ethnicity = c(rep(c("White", "Asian", "Black", "Hispanic", "Other"), times=20/5)),
Age = c(1:20),
Set = rep(c(1,2,3,4), times=20/4)
)
Is there a way to filter out all the rows where Ethnicity
is NOT Asian using the filter
function in R? Maybe something similar to -filter
?
Thanks!
CodePudding user response:
In addition to PaulS's tidyverse/dplyr solution, here's how you can filter out rows using base R.
df[df$Ethnicity != "Asian",]
#> ID Ethnicity Age Set
#> 1 1 White 1 1
#> 3 3 Black 3 3
#> 4 4 Hispanic 4 4
#> 5 5 Other 5 1
#> 6 6 White 6 2
#> 8 8 Black 8 4
#> 9 9 Hispanic 9 1
#> 10 10 Other 10 2
#> 11 11 White 11 3
#> 13 13 Black 13 1
#> 14 14 Hispanic 14 2
#> 15 15 Other 15 3
#> 16 16 White 16 4
#> 18 18 Black 18 2
#> 19 19 Hispanic 19 3
#> 20 20 Other 20 4