2.2 Summarizing univariate data

For a video explainer of the code in this section, see below. The video only discusses the code. Use the notes and lecture discussion for additional context. (Via youtube, you can speed up the playback to 1.5 or 2x speed.)

Univariate data refers to data coming from one “variable,” where a variable captures the values of a changing characteristic.

Our set of values is Outcome = {0,0,0,0,1,1,0,1,0,1}.

  • We will call this a vector of values, where a vector is just a collection of things.
  • Because our vector contains only numbers, we will call it a numeric vector.
  • Each value can be indexed by i, denoting the position of the value in the
  • For example, Jesse is in position i=10 of the vector, and his value is 1

We can create vectors in R by using c() and assigning <- it to an object we will call Outcome.

Outcome <- c(0,0,0,0,1,1,0,1,0,1) # Use commas to separate values

We can extract a particular value within our vector using brackets

Outcome[10]
## [1] 1

We can label our outcomes using names()

names(Outcome) <-c("Joe","Sally", "Trevor", "Emily", "Mark",
                   "Sarah Jane", "Stacey", "Steve", "Phoebe", "Jesse")
Outcome[10]
## Jesse 
##     1

We can overwrite whole vectors or values within a vector

Outcome <- c(5,0,2, 6,1,1, 7, 8, 0, 1) # oops we put the wrong numbers
Outcome
##  [1] 5 0 2 6 1 1 7 8 0 1
Outcome <- c(0,0,0,0,1,1,0,1,0,1) # no problem, just overwrite it
Outcome
##  [1] 0 0 0 0 1 1 0 1 0 1

Oops we accidentally type a 0 for Jesse.

Outcome <- c(0,0,0,0,1,1,0,1,0,0) # oops typo for Jesse
Outcome
##  [1] 0 0 0 0 1 1 0 1 0 0
Outcome[10] <- 1 # no prob bob. Assign a 1 in position 10
Outcome
##  [1] 0 0 0 0 1 1 0 1 0 1

Vectors do not have to be numeric. Character vectors contain a collection of words and phrases. In R, we use quotations around character values

Example: let’s create a vector of names that we will call People.

People <- c("Joe","Sally", "Trevor", "Emily", "Mark", "Sarah Jane", "Stacey", "Steve", "Phoebe", "Jesse")
People[10]
## [1] "Jesse"

We can use the R function class() to tell us the type of object we have.

class(Outcome)
## [1] "numeric"
class(People)
## [1] "character"