- Hands-On Data Science with R
- Vitor Bianchi Lanzetta Nataraj Dasgupta Ricardo Anjoleto Farias
- 246字
- 2021-06-10 19:12:36
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.
推薦閱讀
- Mastering Proxmox(Third Edition)
- 機(jī)器學(xué)習(xí)及應(yīng)用(在線實(shí)驗(yàn)+在線自測)
- CSS全程指南
- 群體智能與數(shù)據(jù)挖掘
- 計算機(jī)網(wǎng)絡(luò)應(yīng)用基礎(chǔ)
- 智能工業(yè)報警系統(tǒng)
- Embedded Programming with Modern C++ Cookbook
- 基于單片機(jī)的嵌入式工程開發(fā)詳解
- Building a BeagleBone Black Super Cluster
- 軟件構(gòu)件技術(shù)
- 格蠹匯編
- 學(xué)練一本通:51單片機(jī)應(yīng)用技術(shù)
- Cloudera Hadoop大數(shù)據(jù)平臺實(shí)戰(zhàn)指南
- 大數(shù)據(jù):從基礎(chǔ)理論到最佳實(shí)踐
- SQL Server 2019 Administrator's Guide