官术网_书友最值得收藏!

Filtering with filter

The filter verb can be used to extract a subset of rows matching the filter criteria, as shown:

# Filter states with < 1% Illiteracy (i.e., > 99% literacy) 
filter(tstate, Illiteracy < 1) # Equivalently -> filter(tstate, (100 - Illiteracy) > 99) 
 
# Filter states with < 1% Illiteracy and Income > the mean Income of all states 
# We will apply the AND condition using & 
 
filter(tstate, Illiteracy < 1 & Income > mean(Income)) 
 
# This is the same as using , (comma), multiple parameters are treated as AND 
 
identical(filter(tstate, Illiteracy < 1 & Income > mean(Income)),filter(tstate, Illiteracy < 1, Income > mean(Income))) 
# [1] TRUE 
 
# Filter states with Income > the mean Income of all states OR HS Graduation Rate > 60% 
# We will apply the OR condition using | 
 
filter(tstate, Income > mean(Income) | `HS Grad` > 60) 
 
# Filter for states in the West Region and the above condition (Income > the mean Income of all states OR HS Graduation Rate > 60%) 
 
filter(tstate, (Income > mean(Income) | `HS Grad` > 60) & Region=="West") 
 
# Other related verbs include filter_all, filter_if and filter_at 
# An example for each is given below 
 
# Print names of all numeric column 
filter_all(tstate, all_vars(class(.)=="numeric")) 
 
 
# Filter if ALL row values > 1 using all_vars 
select_if(tstate, is.numeric) %>% filter_all(all_vars(. > 1)) # When all vars > 1 
 
# Filter if ANY row values > 4000 using any_vars 
select_if(tstate, is.numeric) %>% filter_all(any_vars(. > 4000)) # When any vars > 4000  

There are various other ways that filter can be used and more details can be found at the online resources for the same.

主站蜘蛛池模板: 高邑县| 石景山区| 闵行区| 康定县| 两当县| 长沙市| 浦北县| 枝江市| 玉环县| 台中县| 乐业县| 中西区| 罗甸县| 井研县| 阿拉善左旗| 长治县| 监利县| 浮梁县| 喜德县| 桃园市| 朝阳区| 泰州市| 延津县| 潮安县| 泸州市| 理塘县| 剑河县| 罗田县| 莱阳市| 九江县| 图片| 巴青县| 珠海市| 木里| 伊宁县| 肇庆市| 邓州市| 金华市| 泾源县| 郯城县| 九江市|