- 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.
推薦閱讀
- Dreamweaver CS3 Ajax網頁設計入門與實例詳解
- 數據展現的藝術
- PowerShell 3.0 Advanced Administration Handbook
- 21天學通PHP
- Python Artificial Intelligence Projects for Beginners
- 程序設計缺陷分析與實踐
- Python Data Science Essentials
- Blender Compositing and Post Processing
- 電腦主板現場維修實錄
- JSP從入門到精通
- 大數據驅動的設備健康預測及維護決策優化
- 格蠹匯編
- PLC與變頻技術應用
- 樂高創意機器人教程(中級 上冊 10~16歲) (青少年iCAN+創新創意實踐指導叢書)
- PowerPoint 2010幻燈片制作高手速成