dplyr

RENAME


> starwars
# A tibble: 87 x 14
   name  height  mass hair_color skin_color eye_color birth_year sex   gender
                                
 1 Luke…    172    77 blond      fair       blue            19   male  mascu…
 2 C-3PO    167    75 NA         gold       yellow         112   none  mascu…
 3 R2-D2     96    32 NA         white, bl… red             33   none  mascu…
 4 Dart…    202   136 none       white      yellow          41.9 male  mascu…
 5 Leia…    150    49 brown      light      brown           19   fema… femin…
 6 Owen…    178   120 brown, gr… light      blue            52   male  mascu…
 7 Beru…    165    75 brown      light      blue            47   fema… femin…
 8 R5-D4     97    32 NA         white, red red             NA   none  mascu…
 9 Bigg…    183    84 black      light      brown           24   male  mascu…
10 Obi-…    182    77 auburn, w… fair       blue-gray       57   male  mascu…
# … with 77 more rows, and 5 more variables: homeworld , species ,
#   films , vehicles , starships 

> starwars_rename <- rename(starwars, id = name) > starwars_rename
# A tibble: 87 x 14
   id    height  mass hair_color skin_color eye_color birth_year sex   gender
                                
 1 Luke…    172    77 blond      fair       blue            19   male  mascu…
 2 C-3PO    167    75 NA         gold       yellow         112   none  mascu…
 3 R2-D2     96    32 NA         white, bl… red             33   none  mascu…
 4 Dart…    202   136 none       white      yellow          41.9 male  mascu…
 5 Leia…    150    49 brown      light      brown           19   fema… femin…
 6 Owen…    178   120 brown, gr… light      blue            52   male  mascu…
 7 Beru…    165    75 brown      light      blue            47   fema… femin…
 8 R5-D4     97    32 NA         white, red red             NA   none  mascu…
 9 Bigg…    183    84 black      light      brown           24   male  mascu…
10 Obi-…    182    77 auburn, w… fair       blue-gray       57   male  mascu…
# … with 77 more rows, and 5 more variables: homeworld , species ,
#   films , vehicles , starships 

ARRANGE


> starwars
# A tibble: 87 x 14
   name  height  mass hair_color skin_color eye_color birth_year sex   gender
                                
 1 Luke…    172    77 blond      fair       blue            19   male  mascu…
 2 C-3PO    167    75 NA         gold       yellow         112   none  mascu…
 3 R2-D2     96    32 NA         white, bl… red             33   none  mascu…
 4 Dart…    202   136 none       white      yellow          41.9 male  mascu…
 5 Leia…    150    49 brown      light      brown           19   fema… femin…
 6 Owen…    178   120 brown, gr… light      blue            52   male  mascu…
 7 Beru…    165    75 brown      light      blue            47   fema… femin…
 8 R5-D4     97    32 NA         white, red red             NA   none  mascu…
 9 Bigg…    183    84 black      light      brown           24   male  mascu…
10 Obi-…    182    77 auburn, w… fair       blue-gray       57   male  mascu…
# … with 77 more rows, and 5 more variables: homeworld , species ,
#   films , vehicles , starships 
> starwars %>% arrange(height)
# A tibble: 87 x 14
   name  height  mass hair_color skin_color eye_color birth_year sex   gender
                                
 1 Yoda      66    17 white      green      brown            896 male  mascu…
 2 Ratt…     79    15 none       grey, blue unknown           NA male  mascu…
 3 Wick…     88    20 brown      brown      brown              8 male  mascu…
 4 Dud …     94    45 none       blue, grey yellow            NA male  mascu…
 5 R2-D2     96    32 NA         white, bl… red               33 none  mascu…
 6 R4-P…     96    NA none       silver, r… red, blue         NA none  femin…
 7 R5-D4     97    32 NA         white, red red               NA none  mascu…
 8 Sebu…    112    40 none       grey, red  orange            NA male  mascu…
 9 Gasg…    122    NA none       white, bl… black             NA male  mascu…
10 Watto    137    NA black      blue, grey yellow            NA male  mascu…
# … with 77 more rows, and 5 more variables: homeworld , species ,
#   films , vehicles , starships 
> starwars %>% arrange(desc(height))
# A tibble: 87 x 14
   name  height  mass hair_color skin_color eye_color birth_year sex   gender
                                
 1 Yara…    264    NA none       white      yellow          NA   male  mascu…
 2 Tarf…    234   136 brown      brown      blue            NA   male  mascu…
 3 Lama…    229    88 none       grey       black           NA   male  mascu…
 4 Chew…    228   112 brown      unknown    blue           200   male  mascu…
 5 Roos…    224    82 none       grey       orange          NA   male  mascu…
 6 Grie…    216   159 none       brown, wh… green, y…       NA   male  mascu…
 7 Taun…    213    NA none       grey       black           NA   fema… femin…
 8 Rugo…    206    NA none       green      orange          NA   male  mascu…
 9 Tion…    206    80 none       grey       black           NA   male  mascu…
10 Dart…    202   136 none       white      yellow          41.9 male  mascu…
# … with 77 more rows, and 5 more variables: homeworld , species ,
#   films , vehicles , starships 

FILTER


> mtcars_t <- as_tibble(mtcars)  > mtcars_t
# A tibble: 32 x 11
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
             
 1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
 2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
 3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
 4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
 5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
 6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
 7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
 8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
 9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
# … with 22 more rows

# filter로 mpg가 30 이상인 자동차를 선택한다.  
> filter(mtcars_t, mpg >= 30)
# A tibble: 4 x 11
    mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
            
1  32.4     4  78.7    66  4.08  2.2   19.5     1     1     4     1
2  30.4     4  75.7    52  4.93  1.62  18.5     1     1     4     2
3  33.9     4  71.1    65  4.22  1.84  19.9     1     1     4     1
4  30.4     4  95.1   113  3.77  1.51  16.9     1     1     5     2

# 예제 데이터 airquality에서 Ozone이나 Solar.R이 결측값인 관찰값을 선택한다. 

> airquality_t <- as_tibble(airquality) > head(airquality_t, n=5)
# A tibble: 5 x 6
  Ozone Solar.R  Wind  Temp Month   Day
         
1    41     190   7.4    67     5     1
2    36     118   8      72     5     2
3    12     149  12.6    74     5     3
4    18     313  11.5    62     5     4
5    NA      NA  14.3    56     5     5
> filter(airquality_t, is.na(Ozone) | is.na(Solar.R))
# A tibble: 42 x 6
   Ozone Solar.R  Wind  Temp Month   Day
          
 1    NA      NA  14.3    56     5     5
 2    28      NA  14.9    66     5     6
 3    NA     194   8.6    69     5    10
 4     7      NA   6.9    74     5    11
 5    NA      66  16.6    57     5    25
 6    NA     266  14.9    58     5    26
 7    NA      NA   8      57     5    27
 8    NA     286   8.6    78     6     1
 9    NA     287   9.7    74     6     2
10    NA     242  16.1    67     6     3
# … with 32 more rows

SELECT


> starwars_sel <- select(starwars, height, gender) 
> starwars_sel
# A tibble: 87 x 2
   height gender   
         
 1    172 masculine
 2    167 masculine
 3     96 masculine
 4    202 masculine
 5    150 feminine 
 6    178 masculine
 7    165 feminine 
 8     97 masculine
 9    183 masculine
10    182 masculine
# … with 77 more rows

MUTATE


> head(cars)
  speed dist
1     4    2
2     4   10
3     7    4
4     7   22
5     8   16
6     9   10

> cars %>% mutate(result = speed*dist) %>% head()
  speed dist result
1     4    2      8
2     4   10     40
3     7    4     28
4     7   22    154
5     8   16    128
6     9   10     90

> head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

> iris01 <- iris > iris01[,c("SLPL", "SWPW")] <- NA > str(iris01)
'data.frame':	150 obs. of  7 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ SLPL        : logi  NA NA NA NA NA NA ...
 $ SWPW        : logi  NA NA NA NA NA NA ...

> iris01 <- mutate(iris01, SLPL = iris$Sepal.Length / iris$Petal.Length, SWPW = iris$Sepal.Width / iris$Petal.Width) 

> head(iris01)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species     SLPL  SWPW
1          5.1         3.5          1.4         0.2  setosa 3.642857 17.50
2          4.9         3.0          1.4         0.2  setosa 3.500000 15.00
3          4.7         3.2          1.3         0.2  setosa 3.615385 16.00
4          4.6         3.1          1.5         0.2  setosa 3.066667 15.50
5          5.0         3.6          1.4         0.2  setosa 3.571429 18.00
6          5.4         3.9          1.7         0.4  setosa 3.176471  9.75