面向 SAS 用户的 R
Melinda Higgins, PhD
Research Professor/Senior Biostatistician Emory University
# 用 [] 选择 x 的第3个元素
x <- c(5,3,2)
x[3]
结果
2

# 选择第1行第2列的值
m[1, 2]
结果
4.1

# 行留空,选择第2列
m[, 2]
结果
4.1 2.2 5.4

# 选择第3行,列留空
m[3, ]
结果
2.4 5.4

# 选择第2行第3列元素
d[2, 3]
FALSE
# 选择 d 的第2列
d[, 2]
child young old
# 选择 d 的第3行
d[3, ]
2.4 old TRUE

# 按名称选择 d 的 test 变量
d[,"test"]
结果
[1] TRUE FALSE TRUE

# 用 pull() 获取 test 列
d %>% pull(test)
结果
TRUE FALSE TRUE

# 用 select() 获取 age 到 test
d %>% select(age:test)
# 从 d 提取 test 和 score
d %>% select(test, score)
age test
1 child TRUE
2 young FALSE
3 old TRUE
test score
1 TRUE 5.0
2 FALSE 3.1
3 TRUE 2.4
# 用 slice() 获取第2到3行
d %>% slice(2:3)
结果
score age test
3.1 young FALSE
2.4 old TRUE

# 查看 d
d
# 提取 d 的第2行
d %>% slice(2)
# 提取 d 的第3和第1行
d %>% slice(c(3,1))
score age test
1 5.0 child TRUE
2 3.1 young FALSE
3 2.4 old TRUE
score age test
1 3.1 young FALSE
score age test
1 2.4 old TRUE
2 5.0 child TRUE
面向 SAS 用户的 R