SAS ユーザーのための R
Melinda Higgins, PhD
Research Professor/Senior Biostatistician Emory University

x を作成x に値4を代入x の内容を確認[1] は x が1要素であることを示すy を x の2乗として作成y と入力して結果を確認x <- 4
x
[1] 4
y <- x * x
y
[1] 16
y に代入FALSE を z に代入y と z を確認y <- "fish"
z <- FALSE
y
z
[1] "fish"
[1] FALSE
# Combine three numbers
c(5, 3, 2)

# Assign numeric vector to x
x <- c(5, 3, 2)
# View result
x
結果
[1] 5 3 2

# Add word child into character vector
y <- c("child")

# Add second word young
y <- c("child", "young")

# Add third word old
y <- c("child", "young", "old")
# View y
y
結果
[1] "child" "young" "old"

# Add TRUE into logical vector
z <- c(TRUE)
TRUETRUE または FALSEすべて大文字 を使用することT や F も使用可能
# Add FALSE as second element
z <- c(TRUE, FALSE)

# Add third element TRUE
z <- c(TRUE, FALSE, TRUE)
# View z
z
結果
[1] TRUE FALSE TRUE

# Create numeric vector a
a <- c(5.0, 3.1, 2.4)

# Create numeric vector a
a <- c(5.0, 3.1, 2.4)
# Create numeric vector b
b <- c(4.1, 2.2, 5.4)

# Make m from a,b with 3 rows 2 columns
m <- matrix(c(a, b),
nrow = 3,
ncol = 2)
# View m
m
[,1] [,2]
[1,] 5.0 4.1
[2,] 3.1 2.2
[3,] 2.4 5.4

# Create numeric variable score
score <- c(5.0, 3.1, 2.4)
# View score
score
5.0 3.1 2.4
# Create character variable age
age <- c("child","young","old")
# View age
age
"child" "young" "old"
# Create logical variable test
test <- c(TRUE, FALSE, TRUE)
# View test
test
TRUE FALSE TRUE
# Combine to create data frame
d <- data.frame(score, age, test)
# View data frame
d
score age test
5.0 child TRUE
3.1 young FALSE
2.4 old TRUE

x を作成x の class を確認x の構造 str を確認x <- c(5, 3, 2)
class(x)
[1] "numeric"
str(x)
num [1:3] 5 3 2
y;論理ベクトル zy と z の class を確認y と z の構造 str を確認str(y)
chr [1:3] "child" "young" "old"
str(z)
logi [1:3] TRUE FALSE TRUE
y <- c("child","young","old")
z <- c(TRUE, FALSE, TRUE)
class(y)
[1] "character"
class(z)
[1] "logical"
m を作成m の class を取得m の構造 str を確認str(m)
num [1:3, 1:2] 5 3.1 2.4 4.1 2.2 5.4
a <- c(5.0, 3.1, 2.4)
b <- c(4.1, 2.2, 5.4)
m <- matrix(c(a, b),
nrow = 3,
ncol = 2)
class(m)
[1] "matrix"
data.frame d を作成d の class を取得d の構造 str を確認str(d)
'data.frame': 3 obs. of 3 variables:
$ score: num 5 3.1 2.4
$ age : Factor w/ 3 levels
"child","old",..: 1 3 2
$ test : logi TRUE FALSE TRUE
score <- c(5.0, 3.1, 2.4)
age <- c("child","young","old")
test <- c(TRUE, FALSE, TRUE)
d <- data.frame(score, age, test)
class(d)
[1] "data.frame"
SAS ユーザーのための R