install.packages(c(“tidyverse”,“ggplot2movies”, “nycflights13”,“odbc”, “writexl”, “openxlsx”, “gapminder”, “usethis”,“pacman”))

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()
library(nycflights13)
library(openxlsx)
library(gapminder)
library(ggplot2movies)
library(writexl)

library(pacman)
p_load(usethis, tidyverse, nycflights13, openxlsx, gapminder, ggplot2movies, writexl)

Q1.

請用 select() 從資料 movies中 (ggplot2movies), 選出欄位名稱從 title 到 votes, 以及ActionShort的資料。

library(pacman)
p_load(ggplot2movies)
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>
select(movies,title:votes)
## # A tibble: 58,788 x 6
##    title                     year length budget rating votes
##    <chr>                    <int>  <int>  <int>  <dbl> <int>
##  1 $                         1971    121     NA    6.4   348
##  2 $1000 a Touchdown         1939     71     NA    6      20
##  3 $21 a Day Once a Month    1941      7     NA    8.2     5
##  4 $40,000                   1996     70     NA    8.2     6
##  5 $50,000 Climax Show, The  1975     71     NA    3.4    17
##  6 $pent                     2000     91     NA    4.3    45
##  7 $windle                   2002     93     NA    5.3   200
##  8 '15'                      2002     25     NA    6.7    24
##  9 '38                       1987     97     NA    6.6    18
## 10 '49-'17                   1917     61     NA    6      51
## # … with 58,778 more rows
library(pacman)
p_load(ggplot2movies)
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>
select(movies,Action:Short)
## # A tibble: 58,788 x 7
##    Action Animation Comedy Drama Documentary Romance Short
##     <int>     <int>  <int> <int>       <int>   <int> <int>
##  1      0         0      1     1           0       0     0
##  2      0         0      1     0           0       0     0
##  3      0         1      0     0           0       0     1
##  4      0         0      1     0           0       0     0
##  5      0         0      0     0           0       0     0
##  6      0         0      0     1           0       0     0
##  7      1         0      0     1           0       0     0
##  8      0         0      0     0           1       0     1
##  9      0         0      0     1           0       0     0
## 10      0         0      0     0           0       0     0
## # … with 58,778 more rows

Q2.

從資料 movies中,找出欄位名稱開頭為 r or m的欄位.

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

Q3.

請將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

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.