R For SAS Users
Melinda Higgins, PhD
Research Professor/Senior Biostatistician Emory University
# Select 3rd element from x using []
x <- c(5,3,2)
x[3]
Result
2
# Select value at row 1 column 2
m[1, 2]
Result
4.1
# Leave row blank, select column 2
m[, 2]
Result
4.1 2.2 5.4
# Select row 3, leave column blank
m[3, ]
Result
2.4 5.4
# Select element at row 2 column 3
d[2, 3]
FALSE
# Select second column from d
d[, 2]
child young old
# Select third row from d
d[3, ]
2.4 old TRUE
# Select test variable from d
d[,"test"]
Result
[1] TRUE FALSE TRUE
# Use pull() get test column
d %>% pull(test)
Result
TRUE FALSE TRUE
# Use select(), get age and test
d %>% select(age:test)
# Extract test and score from d
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
# Use slice(), get rows 2 through 3
d %>% slice(2:3)
Result
score age test
3.1 young FALSE
2.4 old TRUE
# View d
d
# Extract row 2 from d
d %>% slice(2)
# Extract rows 3 and 1 from d
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
R For SAS Users