R語言初步-數(shù)據(jù)轉(zhuǎn)換-3.select()函數(shù)和rename()函數(shù)

R語言學(xué)習(xí)筆記總結(jié)

R語言初步-用dplyr進行數(shù)據(jù)轉(zhuǎn)換

install.packages("tidyverse")
install.packages("nycflights13")#仍然記得要先安裝
library(nycflights13)#航班信息文件
library(tidyverse)

?flights#查看數(shù)據(jù)信息的說明書
flights#查看航班信息

3.使用select()函數(shù)選擇列&用rename()函數(shù)重命名列

select(數(shù)據(jù)框名稱,篩選的列)

3.1、按名稱選擇列

select(flights,year,month,day)
#運行后:
# A tibble: 336,776 x 3
    year month   day
   <int> <int> <int>
 1  2013     1     1
 2  2013     1     1
 3  2013     1     1
 4  2013     1     1
 5  2013     1     1
 6  2013     1     1
 7  2013     1     1
 8  2013     1     1
 9  2013     1     1
10  2013     1     1
# ... with 336,766 more rows

3.2、選擇兩者之間的所有列

用A:B的格式選擇 AB兩者之間,包括AB的列。

select(flights,year:day)
#運行后:
# A tibble: 336,776 x 3
    year month   day
   <int> <int> <int>
 1  2013     1     1
 2  2013     1     1
 3  2013     1     1
 4  2013     1     1
 5  2013     1     1
 6  2013     1     1
 7  2013     1     1
 8  2013     1     1
 9  2013     1     1
10  2013     1     1
# ... with 336,766 more rows

3.3、選擇兩者之外的所有列

用-(A:B)的格式選擇 AB兩者之外,不包括AB的列

select(flights,-(year:day))
#運行后:
# A tibble: 336,776 x 16
   dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay
      <int>          <int>     <dbl>    <int>          <int>     <dbl>
 1      517            515         2      830            819        11
 2      533            529         4      850            830        20
 3      542            540         2      923            850        33
 4      544            545        -1     1004           1022       -18
 5      554            600        -6      812            837       -25
 6      554            558        -4      740            728        12
 7      555            600        -5      913            854        19
 8      557            600        -3      709            723       -14
 9      557            600        -3      838            846        -8
10      558            600        -2      753            745         8
# ... with 336,766 more rows, and 10 more variables: carrier <chr>,
#   flight <int>, tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>,
#   distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm> 

3.3、和select()搭配使用的輔助函數(shù)

  • start_with("xxx") 匹配出列名稱開頭是xxx的列
  • ends_with("xxx") 匹配出列名稱末尾是xxx的列
  • contains("xxx") 匹配出列中包含xxx的列
  • matches(""(.)\1")") 匹配出列名稱中有重復(fù)字符的變量
  • num_range("xxx")
  • everything() 將選中的列移動到數(shù)據(jù)框開頭幾列

3.3.1 start_with()

匹配出列名稱開頭是arr的列

select(flights,starts_with("arr"))
#運行后 :
# A tibble: 336,776 x 2
   arr_time arr_delay
      <int>     <dbl>
 1      830        11
 2      850        20
 3      923        33
 4     1004       -18
 5      812       -25
 6      740        12
 7      913        19
 8      709       -14
 9      838        -8
10      753         8
# ... with 336,766 more rows

3.3.2 ends_with()

匹配出列名稱末尾是time的列

select(flights,ends_with("time"))
#運行后 :
# A tibble: 336,776 x 5
   dep_time sched_dep_time arr_time sched_arr_time air_time
      <int>          <int>    <int>          <int>    <dbl>
 1      517            515      830            819      227
 2      533            529      850            830      227
 3      542            540      923            850      160
 4      544            545     1004           1022      183
 5      554            600      812            837      116
 6      554            558      740            728      150
 7      555            600      913            854      158
 8      557            600      709            723       53
 9      557            600      838            846      140
10      558            600      753            745      138
# ... with 336,766 more rows

3.3.3 contains()

匹配出列中包含dep的列

select(flights,contains("dep"))
#運行后 :
# A tibble: 336,776 x 3
   dep_time sched_dep_time dep_delay
      <int>          <int>     <dbl>
 1      517            515         2
 2      533            529         4
 3      542            540         2
 4      544            545        -1
 5      554            600        -6
 6      554            558        -4
 7      555            600        -5
 8      557            600        -3
 9      557            600        -3
10      558            600        -2
# ... with 336,766 more rows

3.3.4 matches()

匹配出列名稱中有重復(fù)字符的變量
涉及正則表達式的使用,待補充~?

select(flights,matches("(.)\\1"))
#運行后 :
# A tibble: 336,776 x 4
   arr_time sched_arr_time arr_delay carrier
      <int>          <int>     <dbl> <chr>  
 1      830            819        11 UA     
 2      850            830        20 UA     
 3      923            850        33 AA     
 4     1004           1022       -18 B6     
 5      812            837       -25 DL     
 6      740            728        12 UA     
 7      913            854        19 B6     
 8      709            723       -14 EV     
 9      838            846        -8 B6     
10      753            745         8 AA     
# ... with 336,766 more rows

3.3.5 everything()

將選中的列移動到數(shù)據(jù)框開頭幾列

select(flights,dep_time,arr_time,day,month,year,everything())
#運行后:
# A tibble: 336,776 x 19
   dep_time arr_time   day month  year sched_dep_time dep_delay
      <int>    <int> <int> <int> <int>          <int>     <dbl>
 1      517      830     1     1  2013            515         2
 2      533      850     1     1  2013            529         4
 3      542      923     1     1  2013            540         2
 4      544     1004     1     1  2013            545        -1
 5      554      812     1     1  2013            600        -6
 6      554      740     1     1  2013            558        -4
 7      555      913     1     1  2013            600        -5
 8      557      709     1     1  2013            600        -3
 9      557      838     1     1  2013            600        -3
10      558      753     1     1  2013            600        -2
# ... with 336,766 more rows, and 12 more variables: sched_arr_time <int>,
#   arr_delay <dbl>, carrier <chr>, flight <int>, tailnum <chr>,
#   origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
#   minute <dbl>, time_hour <dttm>

3.3.5 num_range()

本文使用的數(shù)據(jù)表不適用此函數(shù),暫不舉例
大致用法:num_range("x",1),可以 匹配x1、x2和x3

補充:用rename()函數(shù)重命名列

select()函數(shù) 也可以重命名列,但是這樣做是不推薦的,因為select改變了原始數(shù)據(jù)。所以應(yīng)該選擇使用rename()函數(shù)

用法:rename(數(shù)據(jù)框名稱,重命名=原名)
注意:兩個名字不要寫反,否則無法識別 。
舉例:將year重命名為y,代碼如下。

rename(flights,y = year)
#運行后:
# A tibble: 336,776 x 19
       y month   day dep_time sched_dep_time dep_delay arr_time
   <int> <int> <int>    <int>          <int>     <dbl>    <int>
 1  2013     1     1      517            515         2      830
 2  2013     1     1      533            529         4      850
 3  2013     1     1      542            540         2      923
 4  2013     1     1      544            545        -1     1004
 5  2013     1     1      554            600        -6      812
 6  2013     1     1      554            558        -4      740
 7  2013     1     1      555            600        -5      913
 8  2013     1     1      557            600        -3      709
 9  2013     1     1      557            600        -3      838
10  2013     1     1      558            600        -2      753
# ... with 336,766 more rows, and 12 more variables: sched_arr_time <int>,
#   arr_delay <dbl>, carrier <chr>, flight <int>, tailnum <chr>,
#   origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
#   minute <dbl>, time_hour <dttm>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容