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

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 #
主站蜘蛛池模板: 普格县| 平远县| 平江县| 丰都县| 盱眙县| 那坡县| 大连市| 田阳县| 布拖县| 宁强县| 平定县| 巴彦淖尔市| 普洱| 怀柔区| 宁德市| 菏泽市| 宁津县| 云霄县| 鞍山市| 治多县| 元江| 独山县| 惠来县| 门源| 伊吾县| 涪陵区| 合江县| 兰考县| 邵东县| 辉南县| 大荔县| 东莞市| 凤山市| 青神县| 庆元县| 赞皇县| 武宣县| 黄龙县| 西乌珠穆沁旗| 台安县| 炎陵县|