SAS 사용자를 위한 R
Melinda Higgins, PhD
Research Professor/Senior Biostatistician Emory University

x 생성x에 값 4 할당x 내용 확인[1]은 x가 1개 요소임을 의미y를 x의 제곱으로 생성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
# 숫자 3개 결합
c(5, 3, 2)

# 숫자 벡터를 x에 할당
x <- c(5, 3, 2)
# 결과 보기
x
Result
[1] 5 3 2

# 문자 벡터에 단어 child 추가
y <- c("child")

# 두 번째 단어 young 추가
y <- c("child", "young")

# 세 번째 단어 old 추가
y <- c("child", "young", "old")
# y 보기
y
Result
[1] "child" "young" "old"

# 논리 벡터에 TRUE 추가
z <- c(TRUE)
TRUETRUE 또는 FALSEALL CAPS 사용 필수T, F도 사용 가능
# 두 번째 요소로 FALSE 추가
z <- c(TRUE, FALSE)

# 세 번째 요소 TRUE 추가
z <- c(TRUE, FALSE, TRUE)
# z 보기
z
Result
[1] TRUE FALSE TRUE

# 숫자 벡터 a 생성
a <- c(5.0, 3.1, 2.4)

# 숫자 벡터 a 생성
a <- c(5.0, 3.1, 2.4)
# 숫자 벡터 b 생성
b <- c(4.1, 2.2, 5.4)

# a,b로 3행 2열 행렬 m 생성
m <- matrix(c(a, b),
nrow = 3,
ncol = 2)
# m 보기
m
[,1] [,2]
[1,] 5.0 4.1
[2,] 3.1 2.2
[3,] 2.4 5.4

# 숫자 변수 score 생성
score <- c(5.0, 3.1, 2.4)
# score 보기
score
5.0 3.1 2.4
# 문자 변수 age 생성
age <- c("child","young","old")
# age 보기
age
"child" "young" "old"
# 논리 변수 test 생성
test <- c(TRUE, FALSE, TRUE)
# test 보기
test
TRUE FALSE TRUE
# 결합하여 데이터 프레임 생성
d <- data.frame(score, age, test)
# 데이터 프레임 보기
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