ステム・アンド・リーフ Last modified: Apr 13, 2004
目的
ステム・アンド・リーフを作成する
注: R には stem 関数が用意されている
使用法
my.stem(d, f=-99)
引数
d データベクトル
f 小数点の移動(元の値を10f倍した整数部をstemにする)
省略された場合(-99が仮定され),元の値をそのまま用いる
ソース
インストールは,以下の 1 行をコピーし,R コンソールにペーストする
source("http://aoki2.si.gunma-u.ac.jp/R/src/my_stem.R", encoding="euc-jp")
# ステム・アンド・リーフ
my.stem <- function( d,
f=-99) # f は小数点の移動(元の値を10^f倍した整数部をstemにする)
{
get.factor <- function(x, minx, maxx)
{
for (i in c(1:10, -1:-10)) {
ll <- trunc(maxx*10^i)-trunc(minx*10^i)
if (ll >= 2 && ll < 19) {
return(10^i)
}
}
return(1)
}
DUMMY <- 99 # ダミーの leaf
MINUS <- -0.1 # -0.xxx などの stem
d <- d[!is.na(d)]
f <- ifelse(f == -99, get.factor(d, min(d), max(d)), 10^f)
temp <- trunc(d*f*10)
stem <- trunc(temp/10)
leaf <- abs(temp)-abs(stem*10)
stem <- ifelse(stem == 0, ifelse(d > 0, 0, MINUS), stem)
# 跳んでいる stem を補間する
min.stem <- min(stem)
max.stem <- max(stem)
stem <- c(stem, min.stem:max.stem)
leaf <- c(leaf, rep(DUMMY, max.stem-min.stem+1))
# -0.xxx と +0.yyy が存在しうるとき
if (max.stem > 0 && min.stem < 0) {
stem <- c(stem, MINUS)
leaf <- c(leaf, DUMMY)
}
res <- table(stem, leaf)
sapply(1:nrow(res),
function(i) {
stem.temp <- dimnames(res)$stem[i]
cat(ifelse(as.numeric(stem.temp) == MINUS, "-0", stem.temp), "| ")
sapply(1:ncol(res),
function(le) {
if (dimnames(res)$leaf[le] != DUMMY) {
sapply(rep(dimnames(res)$leaf[le], res[i, le]), cat)
}
}
)
cat("\n")
}
)
cat("stem * ", 1/f, "\n")
}
使用例
d <- runif(20) # 一様乱数を20個発生させている
my.stem(d)
出力結果例
データとして乱数を使っているので,以下の出力結果例は毎回変わる
0 | 1235
1 | 7
2 |
3 | 4579
4 | 25
5 | 8
6 | 167
7 | 169
8 |
9 | 37
stem * 0.1
解説ページ
直前のページへ戻る
E-mail to Shigenobu AOKI