環境設定

0.1 作業環境を整える

  • 最初に、現在のメモリー上の変数の確認
ls()
## [1] "Catch"  "Vessel"
  • 作業を始める前に、不必要な変数を消去
# 現在の環境にある変数の消去
rm(list = ls("all.names" = TRUE))
  • メモリー上の変数の確認
ls()
## character(0)
  • 数字表示を科学的記数法(scientific notation)から10進表記(decimal notation)へ変更
 options(scipen = 999)

0.2 基本パッケージの読み込み

  • tidyverse : データサイエンスのための基本パッケージセット 
    “The tidyverse is an opinionated collection of R packages designed for data science. All packages share an underlying design philosophy, grammar, and data structures.”
# データサイエンスのための基本パッケージセット 
# https://www.tidyverse.org/
library(tidyverse)

# tidyverseの時系列データ用パッケージ
# https://lubridate.tidyverse.org/
library(lubridate)

# tableを奇麗に表示させるパッケージ
library(gt)  # https://gt.rstudio.com/

# インタラクティブtableを表示させるパッケージ
library(DT)  # https://rstudio.github.io/DT/

0.3 その他のパッケージの読み込み

# https://docs.ropensci.org/skimr/
library(skimr)

0.4 作業ディレクトリ-の設定

  • Rが参照しているディレクトリー(場所)を確認

getwd()

  • Rが参照するディレクトリー(場所)を設定

set("/Users/gaku/desktop/QResourceMG")

  • 作業ディレクトリーにあるファイルの確認

dir()

0.5 データの取り込み

  • ここでは岩手県の産地水産卸売市場市況を岩手県水産技術センターが2009年度に整備した 水産情報配信システム「大漁ナビ」から 「ひらめ」の日々の市況データと漁船数のデータを分析します。

  • 読み込むためのパッケージとしてtidyverse構成パッケージの一つで、テーブル形のデータを読み込むreadrをここでは使います。

0.5.1 Data 1 岩手県大漁ナビの市況データの読み込み

  • 市況データを読み込む
Market_Data <- readr::read_csv("tairyo_navi_Flounder_teaching.csv")
## Rows: 193738 Columns: 10
## ─ Column specification ────────────────────────────
## Delimiter: ","
## chr  (4): Market, Gear_Type, Species, Standard
## dbl  (5): Landing_Amount, Landing_Price, Highest_Price, Average_Price, Lowes...
## date (1): Landing_Date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# 最初の3行を表示 (通常の記述法)
head(Market_Data, n = 3)  
## # A tibble: 3 × 10
##   Landing_Date Market Gear_Type Species Standard Landing_Amount Landing_Price
##   <date>       <chr>  <chr>     <chr>   <chr>             <dbl>         <dbl>
## 1 2021-07-03   大船渡 定置網    ヒラメ  山                  5.8          1720
## 2 2021-07-05   大船渡 定置網    ヒラメ  山                  1            1090
## 3 2021-07-05   大船渡 底刺網    ヒラメ  山                  1            1120
## # … with 3 more variables: Highest_Price <dbl>, Average_Price <dbl>,
## #   Lowest_Price <dbl>
# 最初の3行を表示 (tidyverseでのパイプ %>%  を使ったデータを参照する記述法)
Market_Data %>% head(n = 3)  
## # A tibble: 3 × 10
##   Landing_Date Market Gear_Type Species Standard Landing_Amount Landing_Price
##   <date>       <chr>  <chr>     <chr>   <chr>             <dbl>         <dbl>
## 1 2021-07-03   大船渡 定置網    ヒラメ  山                  5.8          1720
## 2 2021-07-05   大船渡 定置網    ヒラメ  山                  1            1090
## 3 2021-07-05   大船渡 底刺網    ヒラメ  山                  1            1120
## # … with 3 more variables: Highest_Price <dbl>, Average_Price <dbl>,
## #   Lowest_Price <dbl>

0.5.2 Data 2 岩手県大漁ナビの入港漁船数がある日報データの読み込み

  • 入港隻数データを読み込む
Data_with_Vessel <- readr::read_csv("Tairyonavi_Daily_Flounder16Sep21.csv")
## New names:
## * `` -> ...1
## Rows: 20343 Columns: 10
## ─ Column specification ────────────────────────────
## Delimiter: ","
## chr (6): Date, Market, Gear_Type, Species, Highest_Price, Lowest_Price
## dbl (4): ...1, Number_of_Vessels, Landing_Amount, Average_Price
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Data_with_Vessel %>% tail(n = 5) # 最後の5行を表示
## # A tibble: 5 × 10
##    ...1 Date       Market Gear_Type Number_of_Vessels Species Landing_Amount
##   <dbl> <chr>      <chr>  <chr>                 <dbl> <chr>            <dbl>
## 1 69372 2020-9-8   八木   立て縄                    1 ヒラメ               2
## 2 69373 2020-11-13 八木   立て縄                    1 ヒラメ               2
## 3 69374 2020-9-19  八木   立て縄                    1 ヒラメ               2
## 4 69375 2021-8-26  八木   立て縄                    1 ヒラメ               3
## 5 69376 2021-8-24  八木   立て縄                    1 ヒラメ               1
## # … with 3 more variables: Highest_Price <chr>, Average_Price <dbl>,
## #   Lowest_Price <chr>
  • 二つのデータをhtml(ウエブ)上で確認するためのDTパッケージを使って表示。データが大きいの一部のみランダムに100データ取り出して表示。
Market_Data %>%  sample_n(size = 100) %>%
              DT::datatable(extensions = c('Scroller'),
              options = list(#  これ以下はうまく表示させるためのjava scriptのoption
                      deferRender = TRUE,
                      list(className = 'dt-center',
                      targets = 10),
                      #dom = 'tB',
                      croller = TRUE,
                      scrollCollapse = TRUE,
                      scrollx = TRUE,
                      scrollY = 200,
                      pageLength = 5),
             #add the fillContainer=T before or after options.
                     fillContainer = T,
                     class = "display"
               )
Data_with_Vessel %>% sample_n(size = 100) %>%
             DT::datatable(extensions = c('Scroller'),
            options = list(#  これ以下はうまく表示させるためのjava scriptのoption
                      deferRender = TRUE,
                      list(className = 'dt-center',targets = 10),
                      croller = TRUE,
                      scrollCollapse = TRUE,
                      scrollx = TRUE,
                      scrollY = 200,
                      pageLength = 5),
             #add the fillContainer=T before or after options.
                     fillContainer = T,
                     class = "display"
               )