ref: R语言data.frame常用操作
ref: Keep rows that match a condition
The filter()
function is used to subset a data frame, retaining all rows that satisfy your conditions. To be retained, the row must produce a value of TRUE
for all conditions. Note that when a condition evaluates to NA
the row will be dropped, unlike base subsetting with [
.
Examples
# Filtering by one criterion
filter(starwars, species == "Human")
filter(starwars, mass > 1000)
# Filtering by multiple criteria within a single logical expression
filter(starwars, hair_color == "none" & eye_color == "black")
filter(starwars, hair_color == "none" | eye_color == "black")
# When multiple expressions are used, they are combined using &
filter(starwars, hair_color == "none", eye_color == "black")