#5 分析環境
# 変数の消去
rm(list = ls())

# パッケージ `pacman`を使って必要なパッケージをインストール
if(!require("pacman")) install.packages("pacman")
##  要求されたパッケージ pacman をロード中です
pacman::p_load("tidyverse", 
               "gt",
               "skimr",
               "showtext")

# 表示を科学表示から変更
 options(scipen = 999)
 
#   日本語フォントの追加
 # フォントの追加と設定(適宜日本語フォントを指定してください)
font_add_google("Noto Sans JP", "noto")
showtext::showtext_auto()

#6 データの読み込み
# CSVファイルを読み込む
dat_hirame <- read_csv("qfr_2024_hirame_2.csv")
## Rows: 3000 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (6): Date, Market, Gear_Type, Species, Highest_Price, Lowest_Price
## dbl (3): 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.
#7 データの概要
#7.1 str
# データの構造を確認
dat_hirame |> str()
## spc_tbl_ [3,000 × 9] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ Date             : chr [1:3000] "2018-12-17" "2008-8-28" "1997-9-1" "2008-11-27" ...
##  $ Market           : chr [1:3000] "宮古" "大船渡" "大船渡" "大船渡" ...
##  $ Gear_Type        : chr [1:3000] "その他" "底刺網" "その他" "底刺網" ...
##  $ Number_of_Vessels: num [1:3000] 1 7 12 5 11 2 1 1 2 8 ...
##  $ Species          : chr [1:3000] "ヒラメ" "ヒラメ" "ヒラメ" "ヒラメ" ...
##  $ Landing_Amount   : num [1:3000] 3 48 88 41 65 14 9 1 3 16 ...
##  $ Highest_Price    : chr [1:3000] "1000" "2800" "5500" "2000" ...
##  $ Average_Price    : num [1:3000] 1000 1368 1922 1190 1506 ...
##  $ Lowest_Price     : chr [1:3000] "1000" "200" "1200" "500" ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   Date = col_character(),
##   ..   Market = col_character(),
##   ..   Gear_Type = col_character(),
##   ..   Number_of_Vessels = col_double(),
##   ..   Species = col_character(),
##   ..   Landing_Amount = col_double(),
##   ..   Highest_Price = col_character(),
##   ..   Average_Price = col_double(),
##   ..   Lowest_Price = col_character()
##   .. )
##  - attr(*, "problems")=<externalptr>
#7.2 summary
# データの記述統計量を確認
dat_hirame |> summary()
##      Date              Market           Gear_Type         Number_of_Vessels
##  Length:3000        Length:3000        Length:3000        Min.   : 1.000   
##  Class :character   Class :character   Class :character   1st Qu.: 2.000   
##  Mode  :character   Mode  :character   Mode  :character   Median : 4.000   
##                                                           Mean   : 4.603   
##                                                           3rd Qu.: 6.000   
##                                                           Max.   :32.000   
##                                                                            
##    Species          Landing_Amount     Highest_Price      Average_Price   
##  Length:3000        Min.   : -100.00   Length:3000        Min.   :-30000  
##  Class :character   1st Qu.:    5.00   Class :character   1st Qu.:   924  
##  Mode  :character   Median :   14.00   Mode  :character   Median :  1345  
##                     Mean   :   33.44                      Mean   :  1554  
##                     3rd Qu.:   31.00                      3rd Qu.:  1932  
##                     Max.   :20000.00                      Max.   :  9313  
##                     NA's   :39                            NA's   :31      
##  Lowest_Price      
##  Length:3000       
##  Class :character  
##  Mode  :character  
##                    
##                    
##                    
## 
#7.3 skim
# データの概要
dat_hirame |> skimr::skim()
Data summary
Name dat_hirame
Number of rows 3000
Number of columns 9
_______________________
Column type frequency:
character 6
numeric 3
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
Date 0 1.00 8 10 0 2634 0
Market 0 1.00 2 3 0 3 0
Gear_Type 0 1.00 3 3 0 3 0
Species 0 1.00 2 6 0 4 0
Highest_Price 31 0.99 1 5 0 301 0
Lowest_Price 31 0.99 1 4 0 309 0

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
Number_of_Vessels 0 1.00 4.60 3.75 1 2 4 6 32 ▇▂▁▁▁
Landing_Amount 39 0.99 33.44 377.45 -100 5 14 31 20000 ▇▁▁▁▁
Average_Price 31 0.99 1554.21 1121.89 -30000 924 1345 1932 9313 ▁▁▁▇▆
#8 データの変換
dat_hirame <- dat_hirame |>
              dplyr::mutate(Date = as.Date(Date, format = "%Y-%m-%d"),    #  日付型変換
                     Highest_Price = as.numeric(Highest_Price),          #最高価格の数値型への変換
                     Lowest_Price = as.numeric(Lowest_Price))            #  最低価格の数値型への変換
## Warning: There were 2 warnings in `dplyr::mutate()`.
## The first warning was:
## ℹ In argument: `Highest_Price = as.numeric(Highest_Price)`.
## Caused by warning:
## !  強制変換により NA が生成されました 
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 1 remaining warning.
#8.1 skim
# データの概要
dat_hirame |> skimr::skim()
Data summary
Name dat_hirame
Number of rows 3000
Number of columns 9
_______________________
Column type frequency:
character 3
Date 1
numeric 5
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
Market 0 1 2 3 0 3 0
Gear_Type 0 1 3 3 0 3 0
Species 0 1 2 6 0 4 0

Variable type: Date

skim_variable n_missing complete_rate min max median n_unique
Date 0 1 1994-05-06 2021-09-06 2012-11-11 2634

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
Number_of_Vessels 0 1.00 4.60 3.75 1 2 4.0 6 32 ▇▂▁▁▁
Landing_Amount 39 0.99 33.44 377.45 -100 5 14.0 31 20000 ▇▁▁▁▁
Highest_Price 96 0.97 2504.76 1515.82 50 1500 2002.5 3000 10000 ▇▆▂▁▁
Average_Price 31 0.99 1554.21 1121.89 -30000 924 1345.0 1932 9313 ▁▁▁▇▆
Lowest_Price 273 0.91 878.43 826.70 1 300 600.0 1200 8500 ▇▁▁▁▁
# 入港隻数の分布
dat_hirame |> 
  ggplot(aes(x = Number_of_Vessels)) +
  geom_histogram(fill = "lightblue", color = "black") +
  labs(title = "入港隻数の分布", x = "入港隻数", y = "頻度") +
   theme(text = element_text(family = "noto"))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

print(dat_hirame)
## # A tibble: 3,000 × 9
##    Date       Market Gear_Type Number_of_Vessels Species Landing_Amount
##    <date>     <chr>  <chr>                 <dbl> <chr>            <dbl>
##  1 2018-12-17 宮古   その他                    1 ヒラメ               3
##  2 2008-08-28 大船渡 底刺網                    7 ヒラメ              48
##  3 1997-09-01 大船渡 その他                   12 ヒラメ              88
##  4 2008-11-27 大船渡 底刺網                    5 ヒラメ              41
##  5 1995-06-03 大船渡 その他                   11 ヒラメ              65
##  6 2017-01-24 大船渡 底刺網                    2 ヒラメ              14
##  7 2010-03-15 釜石   底刺網                    1 ヒラメ               9
##  8 1994-12-22 大船渡 その他                    1 ヒラメ               1
##  9 2020-04-02 大船渡 底刺網                    2 ヒラメ               3
## 10 2010-01-25 大船渡 底刺網                    8 ヒラメ              16
## # ℹ 2,990 more rows
## # ℹ 3 more variables: Highest_Price <dbl>, Average_Price <dbl>,
## #   Lowest_Price <dbl>
#平均価格の分布
dat_hirame |> 
  ggplot(aes(x = Average_Price)) +
  geom_histogram(fill = "white", color = "red") +
  labs(title = "平均価格", x = "平均価格(円/Kg)", y = "頻度") +
   theme(text = element_text(family = "noto"))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 31 rows containing non-finite outside the scale range
## (`stat_bin()`).

#平均価格の分布
dat_hirame |> filter(Average_Price < 1000) |> 
  ggplot(aes(y = Average_Price)) +
  geom_boxplot() +
  labs(title = "平均価格", y = "平均価格(円/Kg)") +
   theme(text = element_text(family = "noto")) 

#平均価格の分布
dat_hirame |> filter(Average_Price < 100) |> 
  ggplot(aes(y = Average_Price)) +
  geom_boxplot() +
  labs(title = "平均価格", y = "平均価格(円/Kg)") +
   theme(text = element_text(family = "noto"))

#平均価格の分布
dat_hirame |> filter(Average_Price < 10000) |> 
  ggplot(aes(y = Average_Price)) +
  geom_boxplot() +
  labs(title = "平均価格", y = "平均価格(円/Kg)") +
   theme(text = element_text(family = "noto")) 

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.