面向 SAS 用户的 R
Melinda Higgins, PhD
Research Professor/Senior Biostatistician Emory University

xxx 的内容[1] 表示 x 有 1 个元素y 为 x 的平方yx <- 4
x
[1] 4
y <- x * x
y
[1] 16
yFALSE 赋给 zy 和 zy <- "fish"
z <- FALSE
y
z
[1] "fish"
[1] FALSE
# 合并三个数字
c(5, 3, 2)

# 将数值向量赋给 x
x <- c(5, 3, 2)
# 查看结果
x
结果
[1] 5 3 2

# 将单词 child 加入字符向量
y <- c("child")

# 添加第二个单词 young
y <- c("child", "young")

# 添加第三个单词 old
y <- c("child", "young", "old")
# 查看 y
y
结果
[1] "child" "young" "old"

# 将 TRUE 加入逻辑向量
z <- c(TRUE)
TRUETRUE 或 FALSEALL CAPST 和 F
# 将 FALSE 作为第二项
z <- c(TRUE, FALSE)

# 添加第三项 TRUE
z <- c(TRUE, FALSE, TRUE)
# 查看 z
z
结果
[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

xx 的 classx 的结构 strx <- c(5, 3, 2)
class(x)
[1] "numeric"
str(x)
num [1:3] 5 3 2
y;逻辑向量 zy 和 z 的 classy 和 z 的结构 strstr(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"
mm 的 classm 的结构 strstr(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 dd 的 classd 的结构 strstr(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