- Hands-On Data Science with R
- Vitor Bianchi Lanzetta Nataraj Dasgupta Ricardo Anjoleto Farias
- 263字
- 2021-06-10 19:12:36
Using select
The verb select, which is part of the dplyr package (installed automatically when the tidyverse package is installed), can be used to select and rename columns from a dataset, as follows:
# dplyr Verbs # select # Add the state name to the tstate dataset tstate$Name <- state.name select(tstate, Income, Frost, Area) # selecting specific columns # # A tibble: 50 x 3 # Income Frost Area # <dbl> <dbl> <dbl> # 1 3624 20 50708 # 2 6315 152 566432 select(tstate, Population:Illiteracy) # selecting a range of columns # # A tibble: 50 x 3 # Population Income Illiteracy # <dbl> <dbl> <dbl> # 1 3615 3624 2.1 # 2 365 6315 1.5 # 3 2212 4530 1.8 select(tstate, -c(Population:Illiteracy)) # excluding a range of columns # # A tibble: 50 x 7 # `Life Exp` Murder `HS Grad` Frost Area Region Name # <dbl> <dbl> <dbl> <dbl> <dbl> <fctr> <chr> # 1 69.05 15.1 41.3 20 50708 South Alabama # 2 69.31 11.3 66.7 152 566432 West Alaska # 3 70.55 7.8 58.1 15 113417 West Arizona rename(tstate, Pop=Population) # renaming specific columns and selecting all columns # # A tibble: 50 x 10 # Pop Income Illiteracy `Life Exp` Murder `HS Grad` Frost Area Region Name # <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <fctr> <chr> # 1 3615 3624 2.1 69.05 15.1 41.3 20 50708 South Alabama # 2 365 6315 1.5 69.31 11.3 66.7 152 566432 West Alaska #
You can also use helper functions such as starts_with, ends_with to select only specific columns matching a criteria, as follows:
select(tstate, starts_with("P"))
# # A tibble: 50 x 1 # Population # <dbl> # 1 3615 # select(tstate, ends_with("n")) # # A tibble: 50 x 2 # Population Region # <dbl> <fctr> # 1 3615 South # 2 365 West #
推薦閱讀
- AWS Administration Cookbook
- 大數(shù)據(jù)驅(qū)動的設(shè)備健康預測及維護決策優(yōu)化
- 從零開始學PHP
- 深度學習與目標檢測
- Linux Shell編程從初學到精通
- 大數(shù)據(jù)案例精析
- Ansible 2 Cloud Automation Cookbook
- C#求職寶典
- JRuby語言實戰(zhàn)技術(shù)
- Creating ELearning Games with Unity
- Arduino創(chuàng)意機器人入門:基于Mind+
- 歐姆龍CP1H型PLC編程與應用
- 洞察大數(shù)據(jù)價值:SAS編程與數(shù)據(jù)挖掘
- 深度學習實戰(zhàn)
- 數(shù)字中國:大數(shù)據(jù)與政府管理決策