
R言語を書かざるをえなくなった人が
R言語の名を知っているぐらいの人が対象
42## [1] 42
の型はわかりますか
42## [1] 42
1要素の数値型ベクトル
ベクトルの挙動を知らずにRは書けない
識別子 <- 式
x <- 1
y <- 1
x + y## [1] 2
->もあるa.b <- 2
1 -> z
a.b + z## [1] 3
現在の環境を表示
ls()## [1] "a.b" "x" "y" "z"
c(TRUE, FALSE, T, F) # 論理値(logical)
c(1L, -2L) # 数値(integer) Lをつけないとdouble
c(1.1, 1e10) # 小数(double)
c("1.1", "a") # 文字列(character)1:25## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25
5番目を取り出す (初めの要素は1番目)
a <- 2:7
a[5] ## [1] 6
5番目を変更
a[5] <- 42
a## [1] 2 3 4 5 42 7
スカラー(値)は1要素のベクトル
5## [1] 5
1要素のベクトルの1要素目の1要素目の1要素目
42[1][1][1]## [1] 42
c(2:5,3:1) # Concat(結合)のC## [1] 2 3 4 5 3 2 1
1要素のベクトルの結合
c(1,4,3) # ベクトルのリテラル表現## [1] 1 4 3
a <- 1:3
b <- 2:3
c(a, b, c(1,3,5))## [1] 1 2 3 2 3 1 3 5
ベクトルは同じ型を持つように型変換される
c(TRUE, 1, 1.1)## [1] 1.0 1.0 1.1
c(1:5, "a")## [1] "1" "2" "3" "4" "5" "a"
NULL## NULL
c(NULL, 1:4, NULL)## [1] 1 2 3 4
c(1,2,NA,4)## [1] 1 2 NA 4
NA == NA## [1] NA
ベクトルを与えることで要素ごとに計算する
sqrt(2)## [1] 1.414214
sqrt(c(1,4,9,16,25))## [1] 1 2 3 4 5
map演算
1 + 3## [1] 4
c(1,2,3) + c(2,4,6)## [1] 3 6 9
c(1,1,1) == c(3,1,2)## [1] FALSE TRUE FALSE
要素が足りない場合, 繰り返して補う
1:5 + 3 # 1:5 + c(3,3,3,3,3)## [1] 4 5 6 7 8
1:2 + 1:6 # c(1,2,1,2,1,2) + 1:6## [1] 2 4 4 6 6 8
c(3,1,2) == 1 # c(3,1,2) == c(1,1,1)## [1] FALSE TRUE FALSE
ベクトルを取ってスカラーを返す関数
mean(1:10)## [1] 5.5
var(1:10)## [1] 9.166667
モンテカルロ法で円周率を求める例
s <- 1000
sum(runif(s)^2+runif(s)^2 <= 1)*4/s## [1] 3.048
ランダムに点をまいて, 四分円の内側を数える 
runif(5) # Random UNIFication## [1] 0.99240864 0.09130816 0.20068849 0.85771570 0.32205799
sum(c(T,F,T,T,F)) # Tは1 Fは0に型変換## [1] 3
sum(runif(s)^2+runif(s)^2 <= 1)*4/s## [1] 3.12
res <- 0
for(i in 1:s){
res <- res + (runif(1)^2+runif(1)^2 <= 1)
}
res * 4 / s## [1] 3.248
library(microbenchmark)
s <- 1000
bench <- microbenchmark(vec_f(s), loop_f(s))(microsecond)
| min | lq | mean | median | uq | max | |
|---|---|---|---|---|---|---|
| vec_f | 288 | 295 | 319 | 304 | 310 | 530 |
| loop_f | 11900 | 12300 | 16600 | 12600 | 14200 | 281000 |
functionで作る{}は不要sub <- function(x,y) x - y
sub(5,3)## [1] 2
sub(y = 10, x = 5) # 名前付き引数## [1] -5
(sex <- factor(c("M","F","F"), levels = c("M", "F")))## [1] M F F
## Levels: M F
str(sex)## Factor w/ 2 levels "M","F": 1 2 2
連想配列
(obj <- list(one=1, two=c(1,1,1), three="3"))## $one
## [1] 1
##
## $two
## [1] 1 1 1
##
## $three
## [1] "3"
print_list(obj) #list表示用に定義した関数## $one: 1; $two: c(1, 1, 1); $three: 3
obj$one## [1] 1
obj[["two"]]## [1] 1 1 1
$か[[でアクセスprint_list(attributes(obj)) # List## $names: c("one", "two", "three")
print_list(attributes(sex)) # Factor## $levels: c("M", "F"); $class: factor
strでは見えない情報が見えるclassを利用したジェネリックな関数classによって関数を呼び分けるstr関数は引数によって実際は以下の関数に振り分けられるmethods("str")## [1] str.data.frame* str.Date* str.default*
## [4] str.dendrogram* str.logLik* str.POSIXt*
## [7] str.Rcpp_stack_trace* str.root_criteria* str.uneval*
## [10] str.unit.arithmetic*
## see '?methods' for accessing help and source code
my_method <- function(x) UseMethod("my_method")
my_method.a <- function(o) cat("called by a", o$a)
my_method.default <- function(o) cat("called by defalut")
obj <- list(x = 1)
class(obj) <- "a" # class属性を"a"に
my_method(obj) # my_method.a が呼ばれる## called by a
class(obj) <- "b" # class属性を"b"に
my_method(obj) # 無いのでmy_method.defualtが呼ばれる## called by defalut
num <- c(1,2,3)
class(num)## [1] "numeric"
class(num) <- "Date"
str(num) # 日付に!?## Date[1:3], format: "1970-01-02" "1970-01-03" "1970-01-04"
setClass("Person",
slots=list(name="character", age="integer"))
str(me <- new("Person", name="wass", age=20L))## Formal class 'Person' [package ".GlobalEnv"] with 2 slots
## ..@ name: chr "wass"
## ..@ age : int 20
@でアクセスする
me@name## [1] "wass"
me@age## [1] 20
try(me@weight <- 80)
try(me@age <- "a")
try(class(me) <- "factor")中身がリストのS3の場合, どれも出来てしまう
$でアクセスclass属性を使った簡易ジェネリックS4はS3から少し厳密になったクラス