2025-05-10
Rとは?
RStudio
基本的な使い方
Tidyなデータとデータ処理
グラフ作成
氏名: 伊東宏樹
個人事業主(伊東生態統計研究室)
1991〜2024年まで森林総合研究所勤務(森林生態学)
出版物: 『BUGSで学ぶ階層モデリング入門』『生態学のための階層モデリング』(以上共訳)など
【参考】
2003年3月の日本生態学会の自由集会「デ-タ解析の落とし穴,穴から脱出する計算機ワザ」
R : Copyright 2002, The R Development Core Team
Version 1.6.1 (2002-11-01)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type `license()' or `licence()' for distribution details.
R is a collaborative project with many contributors.
Type `contributors()' for more information.
Type `demo()' for some demos, `help()' for on-line help, or
`help.start()' for a HTML browser interface to help.
Type `q()' to quit R.
> library(survival)
>
> abies <- read.table("abies_l.data", header=T)
> data <- abies[(abies$start %in% 19970101:19971231),]
> data$a <- (1 - data$a) * (1 - data$c)
> fit <- survfit(Surv(mo, a) ~ D + S, data=data)
>
> postscript("abies_l.eps", horizontal=FALSE, height=(8/2.54), width=(8/2.54), pointsize=9)
> plot(fit, xlab="Months", ylab="Survival Rate", main="Abies homolepis (1997)",
+ xlim=c(0,70), ylim=c(0,1), col=c(2,2,4,4), lty=c(1,2,1,2))
> dev.off()
null device
1
統計計算とグラフィックスのためのシステム(言語と環境)
オープンソース
無料でつかえる
豊富なパッケージ(後述)で機能を追加できる
CRANからインストーラーをダウンロード
各OS用パッケージシステム(homebrew, winget, …)からもインストールできる
特定の解析方法などの関数やデータ類をまとめたもの
CRANなどからインストールできる
マニアックな解析方法もたいていある
ユーザーが自分で作って公開できる
Posit社が開発している統合開発環境(IDE)
プロジェクトでファイル群を管理
実際の画面をご覧ください
コマンドラインの操作が苦手な方にはGUI環境もあり
インストール
起動
計算
ヘルプ
データの型
Tidy data(整然データ)
個々の変数 (variable) が1つの列 (column) をなす。
個々の観測 (observation) が1つの行 (row) をなす。
個々の観測の構成単位の類型 (type of observational unit) が1つの表 (table) をなす。
個々の値 (value) が1つのセル (cell) をなす。
最高気温
都市 | 2025/5/1 | 2025/5/2 |
---|---|---|
札幌 | 18.7 | 16.3 |
東京 | 23.8 | 18.7 |
那覇 | 26.0 | 24.6 |
都市 | 日付 | 最高気温 |
---|---|---|
札幌 | 2025/5/1 | 18.7 |
札幌 | 2025/5/2 | 16.3 |
東京 | 2025/5/1 | 23.8 |
東京 | 2025/5/2 | 18.7 |
那覇 | 2025/5/1 | 26.0 |
那覇 | 2025/5/2 | 24.6 |
CSVファイル
標準のread.csv
関数
readrパッケージのread_csv
関数
Excelファイル
read_excel
関数RStudioではメニューからも実行可能
パッケージの読み込み
Tidy data
tidyverseを使ったデータ処理
Rのグラフ作成環境
デフォルトで組み込みのグラフ作成関数群(base graphics)
ggplot2
plotly など
ggplot2パッケージを使用する方法
デフォルトのグラフィック環境(base graphics)を使用する方法
地域コミュニティ
r-wakalang (Slack)
mandaRa (scrapbox.io)