install.packages("tidyverse")
## Installing package into '/usr/local/lib/R/site-library'
## (as 'lib' is unspecified)
library("tidyverse")
## ── Attaching packages ────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.2.1 ✓ purrr 0.3.3
## ✓ tibble 2.1.3 ✓ dplyr 0.8.3
## ✓ tidyr 1.0.0 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.4.0
## ── Conflicts ───────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
install.packages("magritter")
## Installing package into '/usr/local/lib/R/site-library'
## (as 'lib' is unspecified)
## Warning: package 'magritter' is not available (for R version 3.6.2)
library(magrittr)
##
## Attaching package: 'magrittr'
## The following object is masked from 'package:purrr':
##
## set_names
## The following object is masked from 'package:tidyr':
##
## extract
請用 select() 從資料 movies中 (ggplot2movies), 選出欄位名稱從 title 到 votes, 以及 Action 到 Short的資料
library(pacman)
p_load(ggplot2movies,tidyverse)
head(movies)
## # A tibble: 6 x 24
## title year length budget rating votes r1 r2 r3 r4 r5 r6
## <chr> <int> <int> <int> <dbl> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 $ 1971 121 NA 6.4 348 4.5 4.5 4.5 4.5 14.5 24.5
## 2 $100… 1939 71 NA 6 20 0 14.5 4.5 24.5 14.5 14.5
## 3 $21 … 1941 7 NA 8.2 5 0 0 0 0 0 24.5
## 4 $40,… 1996 70 NA 8.2 6 14.5 0 0 0 0 0
## 5 $50,… 1975 71 NA 3.4 17 24.5 4.5 0 14.5 14.5 4.5
## 6 $pent 2000 91 NA 4.3 45 4.5 4.5 4.5 14.5 14.5 14.5
## # … with 12 more variables: r7 <dbl>, r8 <dbl>, r9 <dbl>, r10 <dbl>,
## # mpaa <chr>, Action <int>, Animation <int>, Comedy <int>, Drama <int>,
## # Documentary <int>, Romance <int>, Short <int>
str(movies)
## Classes 'tbl_df', 'tbl' and 'data.frame': 58788 obs. of 24 variables:
## $ title : chr "$" "$1000 a Touchdown" "$21 a Day Once a Month" "$40,000" ...
## $ year : int 1971 1939 1941 1996 1975 2000 2002 2002 1987 1917 ...
## $ length : int 121 71 7 70 71 91 93 25 97 61 ...
## $ budget : int NA NA NA NA NA NA NA NA NA NA ...
## $ rating : num 6.4 6 8.2 8.2 3.4 4.3 5.3 6.7 6.6 6 ...
## $ votes : int 348 20 5 6 17 45 200 24 18 51 ...
## $ r1 : num 4.5 0 0 14.5 24.5 4.5 4.5 4.5 4.5 4.5 ...
## $ r2 : num 4.5 14.5 0 0 4.5 4.5 0 4.5 4.5 0 ...
## $ r3 : num 4.5 4.5 0 0 0 4.5 4.5 4.5 4.5 4.5 ...
## $ r4 : num 4.5 24.5 0 0 14.5 14.5 4.5 4.5 0 4.5 ...
## $ r5 : num 14.5 14.5 0 0 14.5 14.5 24.5 4.5 0 4.5 ...
## $ r6 : num 24.5 14.5 24.5 0 4.5 14.5 24.5 14.5 0 44.5 ...
## $ r7 : num 24.5 14.5 0 0 0 4.5 14.5 14.5 34.5 14.5 ...
## $ r8 : num 14.5 4.5 44.5 0 0 4.5 4.5 14.5 14.5 4.5 ...
## $ r9 : num 4.5 4.5 24.5 34.5 0 14.5 4.5 4.5 4.5 4.5 ...
## $ r10 : num 4.5 14.5 24.5 45.5 24.5 14.5 14.5 14.5 24.5 4.5 ...
## $ mpaa : chr "" "" "" "" ...
## $ Action : int 0 0 0 0 0 0 1 0 0 0 ...
## $ Animation : int 0 0 1 0 0 0 0 0 0 0 ...
## $ Comedy : int 1 1 0 1 0 0 0 0 0 0 ...
## $ Drama : int 1 0 0 0 0 1 1 0 1 0 ...
## $ Documentary: int 0 0 0 0 0 0 0 1 0 0 ...
## $ Romance : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Short : int 0 0 1 0 0 0 0 1 0 0 ...
select(iris,Sepal.Length:Petal.Width)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 5.1 3.5 1.4 0.2
## 2 4.9 3.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9 1.5
## 54 5.5 2.3 4.0 1.3
## 55 6.5 2.8 4.6 1.5
## 56 5.7 2.8 4.5 1.3
## 57 6.3 3.3 4.7 1.6
## 58 4.9 2.4 3.3 1.0
## 59 6.6 2.9 4.6 1.3
## 60 5.2 2.7 3.9 1.4
## 61 5.0 2.0 3.5 1.0
## 62 5.9 3.0 4.2 1.5
## 63 6.0 2.2 4.0 1.0
## 64 6.1 2.9 4.7 1.4
## 65 5.6 2.9 3.6 1.3
## 66 6.7 3.1 4.4 1.4
## 67 5.6 3.0 4.5 1.5
## 68 5.8 2.7 4.1 1.0
## 69 6.2 2.2 4.5 1.5
## 70 5.6 2.5 3.9 1.1
## 71 5.9 3.2 4.8 1.8
## 72 6.1 2.8 4.0 1.3
## 73 6.3 2.5 4.9 1.5
## 74 6.1 2.8 4.7 1.2
## 75 6.4 2.9 4.3 1.3
## 76 6.6 3.0 4.4 1.4
## 77 6.8 2.8 4.8 1.4
## 78 6.7 3.0 5.0 1.7
## 79 6.0 2.9 4.5 1.5
## 80 5.7 2.6 3.5 1.0
## 81 5.5 2.4 3.8 1.1
## 82 5.5 2.4 3.7 1.0
## 83 5.8 2.7 3.9 1.2
## 84 6.0 2.7 5.1 1.6
## 85 5.4 3.0 4.5 1.5
## 86 6.0 3.4 4.5 1.6
## 87 6.7 3.1 4.7 1.5
## 88 6.3 2.3 4.4 1.3
## 89 5.6 3.0 4.1 1.3
## 90 5.5 2.5 4.0 1.3
## 91 5.5 2.6 4.4 1.2
## 92 6.1 3.0 4.6 1.4
## 93 5.8 2.6 4.0 1.2
## 94 5.0 2.3 3.3 1.0
## 95 5.6 2.7 4.2 1.3
## 96 5.7 3.0 4.2 1.2
## 97 5.7 2.9 4.2 1.3
## 98 6.2 2.9 4.3 1.3
## 99 5.1 2.5 3.0 1.1
## 100 5.7 2.8 4.1 1.3
## 101 6.3 3.3 6.0 2.5
## 102 5.8 2.7 5.1 1.9
## 103 7.1 3.0 5.9 2.1
## 104 6.3 2.9 5.6 1.8
## 105 6.5 3.0 5.8 2.2
## 106 7.6 3.0 6.6 2.1
## 107 4.9 2.5 4.5 1.7
## 108 7.3 2.9 6.3 1.8
## 109 6.7 2.5 5.8 1.8
## 110 7.2 3.6 6.1 2.5
## 111 6.5 3.2 5.1 2.0
## 112 6.4 2.7 5.3 1.9
## 113 6.8 3.0 5.5 2.1
## 114 5.7 2.5 5.0 2.0
## 115 5.8 2.8 5.1 2.4
## 116 6.4 3.2 5.3 2.3
## 117 6.5 3.0 5.5 1.8
## 118 7.7 3.8 6.7 2.2
## 119 7.7 2.6 6.9 2.3
## 120 6.0 2.2 5.0 1.5
## 121 6.9 3.2 5.7 2.3
## 122 5.6 2.8 4.9 2.0
## 123 7.7 2.8 6.7 2.0
## 124 6.3 2.7 4.9 1.8
## 125 6.7 3.3 5.7 2.1
## 126 7.2 3.2 6.0 1.8
## 127 6.2 2.8 4.8 1.8
## 128 6.1 3.0 4.9 1.8
## 129 6.4 2.8 5.6 2.1
## 130 7.2 3.0 5.8 1.6
## 131 7.4 2.8 6.1 1.9
## 132 7.9 3.8 6.4 2.0
## 133 6.4 2.8 5.6 2.2
## 134 6.3 2.8 5.1 1.5
## 135 6.1 2.6 5.6 1.4
## 136 7.7 3.0 6.1 2.3
## 137 6.3 3.4 5.6 2.4
## 138 6.4 3.1 5.5 1.8
## 139 6.0 3.0 4.8 1.8
## 140 6.9 3.1 5.4 2.1
## 141 6.7 3.1 5.6 2.4
## 142 6.9 3.1 5.1 2.3
## 143 5.8 2.7 5.1 1.9
## 144 6.8 3.2 5.9 2.3
## 145 6.7 3.3 5.7 2.5
## 146 6.7 3.0 5.2 2.3
## 147 6.3 2.5 5.0 1.9
## 148 6.5 3.0 5.2 2.0
## 149 6.2 3.4 5.4 2.3
## 150 5.9 3.0 5.1 1.8
select(movies,title:votes,Action:Short)
## # A tibble: 58,788 x 13
## title year length budget rating votes Action Animation Comedy Drama
## <chr> <int> <int> <int> <dbl> <int> <int> <int> <int> <int>
## 1 $ 1971 121 NA 6.4 348 0 0 1 1
## 2 $100… 1939 71 NA 6 20 0 0 1 0
## 3 $21 … 1941 7 NA 8.2 5 0 1 0 0
## 4 $40,… 1996 70 NA 8.2 6 0 0 1 0
## 5 $50,… 1975 71 NA 3.4 17 0 0 0 0
## 6 $pent 2000 91 NA 4.3 45 0 0 0 1
## 7 $win… 2002 93 NA 5.3 200 1 0 0 1
## 8 '15' 2002 25 NA 6.7 24 0 0 0 0
## 9 '38 1987 97 NA 6.6 18 0 0 0 1
## 10 '49-… 1917 61 NA 6 51 0 0 0 0
## # … with 58,778 more rows, and 3 more variables: Documentary <int>,
## # Romance <int>, Short <int>
movies %>% select(starts_with("r"))
## # A tibble: 58,788 x 12
## rating r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 Romance
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
## 1 6.4 4.5 4.5 4.5 4.5 14.5 24.5 24.5 14.5 4.5 4.5 0
## 2 6 0 14.5 4.5 24.5 14.5 14.5 14.5 4.5 4.5 14.5 0
## 3 8.2 0 0 0 0 0 24.5 0 44.5 24.5 24.5 0
## 4 8.2 14.5 0 0 0 0 0 0 0 34.5 45.5 0
## 5 3.4 24.5 4.5 0 14.5 14.5 4.5 0 0 0 24.5 0
## 6 4.3 4.5 4.5 4.5 14.5 14.5 14.5 4.5 4.5 14.5 14.5 0
## 7 5.3 4.5 0 4.5 4.5 24.5 24.5 14.5 4.5 4.5 14.5 0
## 8 6.7 4.5 4.5 4.5 4.5 4.5 14.5 14.5 14.5 4.5 14.5 0
## 9 6.6 4.5 4.5 4.5 0 0 0 34.5 14.5 4.5 24.5 0
## 10 6 4.5 0 4.5 4.5 4.5 44.5 14.5 4.5 4.5 4.5 0
## # … with 58,778 more rows
movies %>% select(starts_with("m"))
## # A tibble: 58,788 x 1
## mpaa
## <chr>
## 1 ""
## 2 ""
## 3 ""
## 4 ""
## 5 ""
## 6 ""
## 7 "R"
## 8 ""
## 9 ""
## 10 ""
## # … with 58,778 more rows
從資料 movies中,找出欄位名稱開頭為 r or m的欄位
movies %>%
select(starts_with("r"),starts_with("m"))
## # A tibble: 58,788 x 13
## rating r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 Romance
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
## 1 6.4 4.5 4.5 4.5 4.5 14.5 24.5 24.5 14.5 4.5 4.5 0
## 2 6 0 14.5 4.5 24.5 14.5 14.5 14.5 4.5 4.5 14.5 0
## 3 8.2 0 0 0 0 0 24.5 0 44.5 24.5 24.5 0
## 4 8.2 14.5 0 0 0 0 0 0 0 34.5 45.5 0
## 5 3.4 24.5 4.5 0 14.5 14.5 4.5 0 0 0 24.5 0
## 6 4.3 4.5 4.5 4.5 14.5 14.5 14.5 4.5 4.5 14.5 14.5 0
## 7 5.3 4.5 0 4.5 4.5 24.5 24.5 14.5 4.5 4.5 14.5 0
## 8 6.7 4.5 4.5 4.5 4.5 4.5 14.5 14.5 14.5 4.5 14.5 0
## 9 6.6 4.5 4.5 4.5 0 0 0 34.5 14.5 4.5 24.5 0
## 10 6 4.5 0 4.5 4.5 4.5 44.5 14.5 4.5 4.5 4.5 0
## # … with 58,778 more rows, and 1 more variable: mpaa <chr>
請將iris資料中數值欄位,全部改為英吋(即除以2.5),以及將Species欄位的資料全部轉為英文大寫, 並輸出資料集為irisImperial
irisImperial<- iris %>% mutate(Species = toupper(Species),
Sepal.Width = Sepal.Width/2.5,
Sepal.Length = Sepal.Length/2.5,
Petal.Width = Petal.Width/2.5,
Petal.Length = Petal.Length/2.5)
head(irisImperial)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 2.04 1.40 0.56 0.08 SETOSA
## 2 1.96 1.20 0.56 0.08 SETOSA
## 3 1.88 1.28 0.52 0.08 SETOSA
## 4 1.84 1.24 0.60 0.08 SETOSA
## 5 2.00 1.44 0.56 0.08 SETOSA
## 6 2.16 1.56 0.68 0.16 SETOSA
iris %>%
transmute(Sepal.Width=floor(Sepal.Width),
Species = case_when(
Species == "setosa" ~toupper(Species),
Species == "versicolor" ~toupper(Species),
Species == "virginica" ~toupper(Species),
TRUE ~ as.character(Species)
))
## Sepal.Width Species
## 1 3 SETOSA
## 2 3 SETOSA
## 3 3 SETOSA
## 4 3 SETOSA
## 5 3 SETOSA
## 6 3 SETOSA
## 7 3 SETOSA
## 8 3 SETOSA
## 9 2 SETOSA
## 10 3 SETOSA
## 11 3 SETOSA
## 12 3 SETOSA
## 13 3 SETOSA
## 14 3 SETOSA
## 15 4 SETOSA
## 16 4 SETOSA
## 17 3 SETOSA
## 18 3 SETOSA
## 19 3 SETOSA
## 20 3 SETOSA
## 21 3 SETOSA
## 22 3 SETOSA
## 23 3 SETOSA
## 24 3 SETOSA
## 25 3 SETOSA
## 26 3 SETOSA
## 27 3 SETOSA
## 28 3 SETOSA
## 29 3 SETOSA
## 30 3 SETOSA
## 31 3 SETOSA
## 32 3 SETOSA
## 33 4 SETOSA
## 34 4 SETOSA
## 35 3 SETOSA
## 36 3 SETOSA
## 37 3 SETOSA
## 38 3 SETOSA
## 39 3 SETOSA
## 40 3 SETOSA
## 41 3 SETOSA
## 42 2 SETOSA
## 43 3 SETOSA
## 44 3 SETOSA
## 45 3 SETOSA
## 46 3 SETOSA
## 47 3 SETOSA
## 48 3 SETOSA
## 49 3 SETOSA
## 50 3 SETOSA
## 51 3 VERSICOLOR
## 52 3 VERSICOLOR
## 53 3 VERSICOLOR
## 54 2 VERSICOLOR
## 55 2 VERSICOLOR
## 56 2 VERSICOLOR
## 57 3 VERSICOLOR
## 58 2 VERSICOLOR
## 59 2 VERSICOLOR
## 60 2 VERSICOLOR
## 61 2 VERSICOLOR
## 62 3 VERSICOLOR
## 63 2 VERSICOLOR
## 64 2 VERSICOLOR
## 65 2 VERSICOLOR
## 66 3 VERSICOLOR
## 67 3 VERSICOLOR
## 68 2 VERSICOLOR
## 69 2 VERSICOLOR
## 70 2 VERSICOLOR
## 71 3 VERSICOLOR
## 72 2 VERSICOLOR
## 73 2 VERSICOLOR
## 74 2 VERSICOLOR
## 75 2 VERSICOLOR
## 76 3 VERSICOLOR
## 77 2 VERSICOLOR
## 78 3 VERSICOLOR
## 79 2 VERSICOLOR
## 80 2 VERSICOLOR
## 81 2 VERSICOLOR
## 82 2 VERSICOLOR
## 83 2 VERSICOLOR
## 84 2 VERSICOLOR
## 85 3 VERSICOLOR
## 86 3 VERSICOLOR
## 87 3 VERSICOLOR
## 88 2 VERSICOLOR
## 89 3 VERSICOLOR
## 90 2 VERSICOLOR
## 91 2 VERSICOLOR
## 92 3 VERSICOLOR
## 93 2 VERSICOLOR
## 94 2 VERSICOLOR
## 95 2 VERSICOLOR
## 96 3 VERSICOLOR
## 97 2 VERSICOLOR
## 98 2 VERSICOLOR
## 99 2 VERSICOLOR
## 100 2 VERSICOLOR
## 101 3 VIRGINICA
## 102 2 VIRGINICA
## 103 3 VIRGINICA
## 104 2 VIRGINICA
## 105 3 VIRGINICA
## 106 3 VIRGINICA
## 107 2 VIRGINICA
## 108 2 VIRGINICA
## 109 2 VIRGINICA
## 110 3 VIRGINICA
## 111 3 VIRGINICA
## 112 2 VIRGINICA
## 113 3 VIRGINICA
## 114 2 VIRGINICA
## 115 2 VIRGINICA
## 116 3 VIRGINICA
## 117 3 VIRGINICA
## 118 3 VIRGINICA
## 119 2 VIRGINICA
## 120 2 VIRGINICA
## 121 3 VIRGINICA
## 122 2 VIRGINICA
## 123 2 VIRGINICA
## 124 2 VIRGINICA
## 125 3 VIRGINICA
## 126 3 VIRGINICA
## 127 2 VIRGINICA
## 128 3 VIRGINICA
## 129 2 VIRGINICA
## 130 3 VIRGINICA
## 131 2 VIRGINICA
## 132 3 VIRGINICA
## 133 2 VIRGINICA
## 134 2 VIRGINICA
## 135 2 VIRGINICA
## 136 3 VIRGINICA
## 137 3 VIRGINICA
## 138 3 VIRGINICA
## 139 3 VIRGINICA
## 140 3 VIRGINICA
## 141 3 VIRGINICA
## 142 3 VIRGINICA
## 143 2 VIRGINICA
## 144 3 VIRGINICA
## 145 3 VIRGINICA
## 146 3 VIRGINICA
## 147 2 VIRGINICA
## 148 3 VIRGINICA
## 149 3 VIRGINICA
## 150 3 VIRGINICA