Mais conteúdo relacionado
Semelhante a R_note_02_ver1.0 (20)
Mais de Satoshi Kume (20)
R_note_02_ver1.0
- 1. Satoshi Kume
140818 ver 1.0
R note 02 on Mac OS X
1. 乱数の生成
> head(A <- runif(10, min=0, max=1)) # 0∼1間の一様分布
> head(B <- rnorm(10000, mean=0, sd=1)) # 正規分布に従う乱数生成関数
# head(): 変数の先頭だけ表示、default=6個
[1] -0.6443054 -0.1996644 0.3180832 -0.2864285 0.4732518 1.4134141
> mean(B); var(B) #「;」 複数行を繋げられる
[1] -0.007418862 # 平均 0 分散 1 に近似される
[1] 0.9759483
> sample(B, 5) # Aから5個の非復元ランダム抽出
> head(C <- rexp(5, rate = 1)) # 指数分布に従う乱数生成関数
- 2. > data(ToothGrowth) # 歯の成長に対するビタミンCの効果
> ToothGrowth # 右のように表示される
> str(ToothGrowth) # データフレームで読み込まれている
'data.frame': 60 obs. of 3 variables:...
2. サンプルデータのロード
3. データ列 & 行の取り出し
> ToothGrowth$len # len行の表示・選択
> ToothGrowth[,1] # 1列目の表示・選択(上記と同じ)
> ToothGrowth[1,] # 1行目の表示・選択
> len <- ToothGrowth$len # len行をオブジェクト「len」
# に格納する
> attach(ToothGrowth) # リスト名を明示する必要がなくなる。
# 以下の3行を実行するのと同じ
> len <- ToothGrowth$len
> supp <- ToothGrowth$supp
> dose <- ToothGrowth$dose
> detach(ToothGrowth) # attachのキャンセル
4. データフレームの要素参照
- 3. 5. 単純集計・クロス集計
> attach(ToothGrowth) # 列名による要素参照
以下のオブジェクトはマスクされています (from ToothGrowth (position 3)) :
dose, len, supp
> table(supp) # suppの単純集計
supp
OJ VC
30 30
> table(ToothGrowth$supp, ToothGrowth$dose) # suppとdoseのクロス集計
0.5 1 2
OJ 10 10 10
VC 10 10 10
> table(supp, dose) # suppとdoseのクロス集計(結果は上と同じ)
dose # doseとsuppの説明が追加される。
supp 0.5 1 2
OJ 10 10 10
VC 10 10 10
- 4. > head(supp.dose.c <- cbind(supp, dose)) # suppとdoseの列結合
supp dose
[1,] 2 0.5
[2,] 2 0.5
[3,] 2 0.5
> head(supp.dose.r <- rbind(supp, dose)) # suppとdoseの行結合
[,1] [,2] [,3] ...
supp 2.0 2.0 2.0 ...
dose 0.5 0.5 0.5 ...
> ToothGrowth1 <- transform(ToothGrowth, x=1) # 列 x の追加
> head(ToothGrowth1)
> ToothGrowth1 <- transform(ToothGrowth, abc=2) # 列 abc の追加
> head(ToothGrowth1)
> ToothGrowth1$def <- 3 # 列 def の追加
> head(ToothGrowth1)
6. 行・列データの結合
- 5. 7. 行・列データの抽出
> data(ToothGrowth)
> (ToothGrowth1 <- subset(ToothGrowth, dose==0.5)) # dose 0.5 行の抽出
> (ToothGrowth2 <- subset(ToothGrowth, supp=="VC")) # supp VC 行の抽出
> (ToothGrowth3 <- subset(ToothGrowth, dose>0.5)) # dose 1 と 2 行の抽出
> (ToothGrowth4 <- subset(ToothGrowth, dose>0.5, c(len, dose)))
# dose 1 と 2 行 かつ len と dose 列の抽出
> (ToothGrowth5 <- ifelse(ToothGrowth, dose>0.5, c(len, dose)))
# dose 1 と 2 行 かつ len と dose 列の抽出
- 6. 8. 作図関数 plot()
> plot(Xdata, Ydata,
col = "black",
type = "p, l, b, c, o, h, s, S, n", pch = 20,
lwd = 2, lty = 1, log = “”,
cex=1.5, cex.axis=1.5, cex.main=1.5,
xlim=c(0, 1), ylim=c(0, 1),
cex.lab=1.5, xlab="X-axis", ylab="Y-axis"); par(new=”F”)
type=””
”p”: 点グラフ, ”l”: 折れ線グラフ, type=”b”: 点・線グラフ, ”c”: 線グラフ, ”h”: 垂線グラフ、, ”s or S”: 階段グラフ
log=””: 対数表示無し(デフォルト), log=”x”: X軸の対数表示, log=”xy”: XY軸の対数表示
xlim=c(0,5): X座標の最小値 0 &最大値 5, ylim=c(-10,10): Y座標の最小値 -10 &最大値 10
col = 1: 作図に用いる色
1: black, 2: red, 3: green, 4: blue, 5: skyblue, 6: violet, 7: yellow, 8: grey, rainbow(12): 虹色12段階,
heat.colors(12): 暖色12段階, cm.colors(12): 寒色12段階, topo.colors(12): 地質色12段階
pch = 1: 作図に用いるシンボル(0~25の整数),
0: 四角, 1: ⃝, 2: △, 3: +, 4: ×, 5: ◇, 6: ▽, 8: *, 15: ■, 16: ●, 17: ▲, 18: #
bg = ””: 背景色の指定, fg = ””: 前面色の指定, bty = ”l”: 枠L字型, bty = ”n”: 無枠型
lwd = ”1”: 線分の幅(値が大きければ、太い), lty =”1”: 線分の形式
0: 線無し, 1: 実線, 2: 破線, 3: ドット線, 4: ドット破線, 5: 長いダッシュ, 6: 2つのダッシュ
cex = ”1.5”: 文字やマーカーの拡大率, cex.axis: 軸の拡大率, cex.lab: ラベルの拡大率, cex.main: タイトルの拡大率,
cex.sub: サブタイトルの拡大率
par(new=”T”): 図を上書きする, par(new=”F”): 図を上書きしない
- 7. -1.0 -0.5 0.0 0.5 1.0
0.00.20.40.60.81.0
x
y1
-1.0 -0.5 0.0 0.5 1.0
-1.0-0.8-0.6-0.4-0.20.0
x
y2
9. 円の作図
> x <- seq(-1,1,0.01)
> y1 <- sqrt(1-x^2)
> y2 <- -sqrt(1-x^2)
> plot(x, y1) ; par(new="T")
> plot(x, y2, col="red")
> x <- seq(-1,1,0.01)
> y1 <- sqrt(1-x^2)
> y2 <- -sqrt(1-x^2)
> plot(x, y1, ylim=c(-1.1,1.1), type="l", lwd=3) ; par(new="T")
> plot(x, y2, col="red", ylim=c(-1.1,1.1), type="l", lwd=3, axes=F, ann=F)
- 8. ① ② ③ ④
⑤ ⑥ ⑦ ⑧
10. plotのいろいろ
> plot(sin, -3*pi, 3*pi, type="l") # ①
> plot(cos, -3*pi, 3*pi, type="l") # ②
> plot(ToothGrowth$len) # ③
> plot(ToothGrowth$supp, ToothGrowth$len) # ④
> plot(ToothGrowth$supp, ToothGrowth$dose) # ⑤
> plot(ToothGrowth$dose, ToothGrowth$len) # ⑥
> plot(ToothGrowth$len, ToothGrowth$supp) # ⑦
> plot(ToothGrowth$len, ToothGrowth$dose) # ⑧
- 9. ②①
11. 作図関数のいろいろ boxplot()
> boxplot(ToothGrowth$len ~ ToothGrowth$supp) # ①
> boxplot(ToothGrowth$len ~ ToothGrowth$dose) # ②
12. 作図関数のいろいろ hist()
> hist(ToothGrowth$len)
- 10. 13. グラフの保存
1. ファイル $ 別名で保存 $ Png/Jpeg $ 名前を付けて保存する。
2. グラフ上 $ Control + C $ パワポ上で、Control + V
> png(filename="file.png",width=400,height=500,pointsize=12,bg="white")
> dev.off() ## 必ず行う!