Objects - the building blocks of R

R For SAS Users

Melinda Higgins, PhD

Research Professor/Senior Biostatistician Emory University

Objects are the building blocks of R

  • Everything in R is an object
  • Single values (scalars)
  • Vectors
  • Matrices
  • Datasets
  • Output from functions

objects abc building blocks

R For SAS Users

Work with single elements

  • Create object x
  • Assign value of 4 to x
  • View contents of x
  • [1] indicates x has 1 element
  • Create y as square of x
  • View result, type y
x <- 4
x
[1] 4
y <- x * x
y
[1] 16
R For SAS Users

Other types of elements

  • Assign the word "fish" to y
  • Assign the logical value FALSE to z
  • View y and z
y <- "fish"
z <- FALSE
y
z
[1] "fish"

[1] FALSE
R For SAS Users

Combine single elements into a vector

# Combine three numbers
c(5, 3, 2)

three numbers 5 3 2

R For SAS Users

Combine single elements into a vector

# Assign numeric vector to x
x <- c(5, 3, 2)

# View result
x

Result

[1] 5 3 2

5 3 2 combined into column vector

R For SAS Users

Create vector of character elements

# Add word child into character vector
y <- c("child")

combine three words child

R For SAS Users

Create vector of character elements

# Add second word young
y <- c("child", "young")

young

R For SAS Users

Create vector of character elements

# Add third word old
y <- c("child", "young", "old")

# View y
y

Result

[1] "child" "young" "old"

old

R For SAS Users

Create vector of logical elements

# Add TRUE into logical vector
z <- c(TRUE)
  • First element is TRUE
  • No double quotes needed
  • Logical elements are TRUE or FALSE
  • ALL CAPS must be used
  • T and F may also be used

combine three logical values true all caps

R For SAS Users

Create vector of logical elements

# Add FALSE as second element
z <- c(TRUE, FALSE)

false all caps

R For SAS Users

Create vector of logical elements

# Add third element TRUE
z <- c(TRUE, FALSE, TRUE)

# View z
z

Result

[1]  TRUE FALSE TRUE

true all caps

R For SAS Users

Create matrix from vectors

#  Create numeric vector a
a <- c(5.0, 3.1, 2.4)
  • Matrix created from vectors
  • Vectors must be same type and length

first vector number 5.0 3.1 2.4

R For SAS Users

Create matrix from vectors

#  Create numeric vector a
a <- c(5.0, 3.1, 2.4)

#  Create numeric vector b
b <- c(4.1, 2.2, 5.4)

second vector 4.1 2.2 5.4

R For SAS Users

Create matrix from vectors

# 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

vectors combined into matrix 3 rows 2 columns

R For SAS Users

Create data frame from vectors

# Create numeric variable score
score <- c(5.0, 3.1, 2.4)

# View score
score
     5.0 3.1 2.4
  • Data frames created from vectors
  • Vectors must be same length
  • Vectors can be different types

numeric vector with numbers 5.0 3.1 2.4

R For SAS Users

Create data frame from vectors

# Create character variable age
age <- c("child","young","old")

# View age
age
     "child" "young" "old"

character vector with words child young old

R For SAS Users

Create data frame from vectors

# Create logical variable test
test <- c(TRUE, FALSE, TRUE)

# View test
test
     TRUE FALSE TRUE

vector logical values true false true

R For SAS Users

Create data frame from vectors

# 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

three vectors combined into data frame

R For SAS Users

Determine object type

  • Create numeric vector x
  • Determine the class of x
  • Look at structure str of x
x <- c(5, 3, 2)
class(x)
 [1] "numeric"
str(x)
 num [1:3] 5 3 2
R For SAS Users

Determine object type

  • Character vector y; logical vector z
  • Determine the class of y and z
  • Look at structure 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"
R For SAS Users

Determine object type

  • Make matrix m
  • Get class of m
  • See structure 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"
R For SAS Users

Determine object type

  • Create data.frame d
  • Get class of d
  • See structure 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"
1 The Factor class type was created here automatically for the 3 age categories.
R For SAS Users

Let's create and manipulate data objects in R

R For SAS Users

Preparing Video For Download...