# 必要なパッケージ
library(readxl)
library(dplyr)
##
## 次のパッケージを付け加えます: 'dplyr'
## 以下のオブジェクトは 'package:stats' からマスクされています:
##
## filter, lag
## 以下のオブジェクトは 'package:base' からマスクされています:
##
## intersect, setdiff, setequal, union
# ファイルパス
file_path <- "バイク年式;燃費 .xlsx"
# シート名を取得
sheet_names <- excel_sheets(file_path)
# シートごとのデータを読み込んで1つにまとめる
all_sheets <- lapply(sheet_names, function(sheet) {
df <- read_excel(file_path, sheet = sheet)
df$メーカー <- sheet # メーカー列を追加
df
})
# すべてのバイクデータを結合
all_bikes <- bind_rows(all_sheets)
# 例:列名の統一や文字列→数値の変換(必要に応じて調整)
all_bikes <- all_bikes %>%
rename(
車種 = 1,
年式 = 2,
燃費 = 3,
タンク容量 = 4,
航続距離 = 5,
排気量 = 6
) %>%
mutate(
年式 = as.numeric(年式),
燃費 = as.numeric(燃費),
タンク容量 = as.numeric(タンク容量),
航続距離 = as.numeric(航続距離),
排気量 = as.numeric(排気量)
)
library(ggplot2)
all_bikes %>%
group_by(メーカー) %>%
summarise(平均燃費 = mean(燃費, na.rm = TRUE)) %>%
ggplot(aes(x = reorder(メーカー, 平均燃費), y = 平均燃費, fill = メーカー)) +
geom_bar(stat = "identity") +
coord_flip() +
labs(title = "メーカー別 平均燃費", x = "メーカー", y = "平均燃費(km/L)") +
theme_minimal()

ggplot(all_bikes, aes(x = 排気量, y = 航続距離, color = メーカー)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "排気量と航続距離の関係", x = "排気量 (cc)", y = "航続距離 (km)") +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 1 row containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_point()`).

library(readxl)
library(dplyr)
# ファイルパス
file_path <- "バイク年式;燃費 .xlsx"
sheet_names <- excel_sheets(file_path)
# データ読み込みとメーカー列追加
all_sheets <- lapply(sheet_names, function(sheet) {
df <- read_excel(file_path, sheet = sheet)
df$メーカー <- sheet
df
})
all_bikes <- bind_rows(all_sheets)
# 必要な列名の標準化(調整が必要な場合あり)
all_bikes <- all_bikes %>%
rename(
車種 = 1,
年式 = 2,
燃費 = 3,
タンク容量 = 4,
航続距離 = 5,
排気量 = 6
) %>%
mutate(
年式 = as.numeric(年式),
燃費 = as.numeric(燃費),
排気量 = as.numeric(排気量)
)
# 排気量帯を分類
all_bikes <- all_bikes %>%
mutate(排気量帯 = case_when(
排気量 <= 125 ~ "原付・小型",
排気量 <= 250 ~ "中型",
排気量 <= 750 ~ "大型",
TRUE ~ "特大型"
))
# 排気量帯別平均燃費を可視化
library(ggplot2)
ggplot(all_bikes, aes(x = 排気量帯, y = 燃費)) +
geom_boxplot(fill = "lightblue") +
labs(title = "排気量帯別 燃費比較", x = "排気量帯", y = "燃費(km/L)") +
theme_minimal()

# 仮に2スト・4スト情報を追加(例:条件や外部データで)
all_bikes <- all_bikes %>%
mutate(ストローク = case_when(
grepl("NSR|TZR|RZ", 車種, ignore.case = TRUE) ~ "2スト",
TRUE ~ "4スト"
))
# ストローク別平均燃費比較
ggplot(all_bikes, aes(x = ストローク, y = 燃費, fill = ストローク)) +
geom_boxplot() +
labs(title = "2スト / 4スト 燃費比較", x = "エンジンタイプ", y = "燃費(km/L)") +
theme_minimal()

# 年式ごとの平均燃費を折れ線で表示
all_bikes %>%
group_by(年式) %>%
summarise(平均燃費 = mean(燃費, na.rm = TRUE)) %>%
ggplot(aes(x = 年式, y = 平均燃費)) +
geom_line(color = "darkgreen") +
geom_point() +
labs(title = "年式別 平均燃費の推移", x = "年式", y = "平均燃費(km/L)") +
theme_minimal()

library(dplyr)
bike_spec_list <- all_bikes %>%
select(メーカー, 車種, 年式, 燃費, タンク容量, 航続距離, 排気量) %>%
distinct() %>%
arrange(メーカー, 車種)
print(bike_spec_list)
## # A tibble: 343 × 7
## メーカー 車種 年式 燃費 タンク容量 航続距離 排気量
## <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 BMW C650GT 2016 21.7 15.5 336. 650
## 2 BMW F700GS 2016 25.6 16 410. 800
## 3 BMW F800GS 2016 25 16 400 800
## 4 BMW F800GS Adventure (2014) 2014 23.3 24 559. 798
## 5 BMW F900GS 2025 23.8 14.5 345. 895
## 6 BMW G310GS 2021 30.3 11.5 348. 313
## 7 BMW K1600GT 2012 17.8 24 427. 1649
## 8 BMW R100R Roadster 1991 20 24 480 980
## 9 BMW R1100RT 1995 21 25 525 1085
## 10 BMW R1200GS Adventure 2014 20 30 600 1169
## # ℹ 333 more rows
library(DT)
datatable(
bike_spec_list,
options = list(pageLength = 15),
caption = "すべての車種とスペック一覧(年式、燃費、タンク容量、航続距離、排気量)"
)