3. for
> for (i in 1:10) cat(pnorm(i)," ")
0.8413447 0.9772499 0.9986501 0.9999683 0.9999997 1 1 1 1 1
for (name in expr_1) expr_2
> for (i in 1:ncol(faithful)){
>
print(c(min(faithful[,i]),max(faithful[,i]),mean(faithful[,i]),median(faithf
ul[,i])))
> }
> xyz = list(42,c(1,2,3),matrix(c(1:4),2,2))
> for (i in 1:length(xyz)) xyz[[i]] = xyz[[i]]+1
[1] 43
[1] 2 3 4
[,1] [,2]
[1,] 2 4
[2,] 3 5
4. while
> while (i < 5){
print(dnorm(i))
i = i+1
}
[1] 0.3989423
[1] 0.2419707
[1] 0.05399097
[1] 0.004431848
[1] 0.0001338302
while (condition) expr
7. Warning: for() loops are used in R code much less often tha
n in
compiled languages. Code that takes a ‘whole object’ view is l
ikely
to be both clearer and faster in R
An introduction to R
(http://cran.r-project.org)
けど。。。
9. 1. Built in functions
数学の手段 R言語の関数
全額 sum
平均 mean
中央値 median
分散 var
共分散 cov
相関 cor
対数 log
値の範囲 range
尺度 scale
R言語の関数はVectorizationということ使って
ベクターのようなオブジェクト一緒に扱って処理します
12. 2. apply, lapply, sapply...
● lapply – apply function over list or vector
● sapply – user-friendly version of lapply
● vapply – similar but return specified values
● rapply – recursive apply
● tapply – apply function to a ragged array
● mapply – apply a function to a multiple list
input
???
output
???
13. 3. plyrのパッケージ
AP PLY + R
パッケージの名前について:
data frame data frame
list array
Input Output 関数名前
ddply
laply
array none a_ply