R For SAS Users
Melinda Higgins, PhD
Research Professor/Senior Biostatistician Emory University
x
x
x
[1]
indicates x
has 1 elementy
as square of xy
x <- 4
x
[1] 4
y <- x * x
y
[1] 16
y
FALSE
to z
y
and 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
Result
[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
Result
[1] "child" "young" "old"
# Add TRUE into logical vector
z <- c(TRUE)
TRUE
TRUE
or FALSE
ALL CAPS
must be usedT
and F
may also be used# Add FALSE as second element
z <- c(TRUE, FALSE)
# Add third element TRUE
z <- c(TRUE, FALSE, TRUE)
# View z
z
Result
[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
class
of x
str
of x
x <- c(5, 3, 2)
class(x)
[1] "numeric"
str(x)
num [1:3] 5 3 2
y
; logical vector z
class
of y
and z
str
of y
and z
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
str
of m
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
class
of d
str
of d
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"
R For SAS Users