Chapter 9 Intro to R objects

By: Nathan Brouwer

9.1 Commands used

  • <-
  • c()
  • length()
  • dim()
  • is()

9.2 R Objects

  • Everything in R is an object, works with an object, tells you about an object, etc
  • We’ll do a simple data analysis with a t.test and then look at properties of R objects
  • There are several types of objects: vectors, matrices, lists, dataframes
  • R objects can hold numbers, text, or both
  • A typical dataframe has columns of numeric data and columns of text that represent factor variables (aka “categorical variables”)

9.3 Differences between objects

Different objects are used and show up in different contexts.

  • Most practical stats work in R is done with dataframes .
  • A dataframe is kind of like a spreadsheet, loaded into R.
  • For the sake of simplicity, we often load data in as a vector. This just makes things smoother when we are starting out.
  • vectors pop up in many places, usually in a support role until you start doing more programming.
  • matrices are occasionally used for applied stats stuff but show up more for programming. A matrix is like a stripped-down dataframe.
  • lists show up everywhere, but you often don’t know it; many R functions make lists
  • Understanding lists will help you efficiently work with stats output and make plots.

9.4 The Data

We’ll use the following data to explore R objects.

Motulsky 2nd Ed, Chapter 30, page 220, Table 30.1. Maximal relaxation of muscle strips of old and young rat bladders stimulated with high concentrations of norepinephrine (Frazier et al. 2006). Response variable is % E.max

9.5 The assignment operator “<-” makes object

The code above has made two objects. We can use several commands to learn about these objects.

  • is(): what an object is, i.e., vector, matrix, list, dataframe
  • length():how long an object is; only works with vectors and lists, not dataframes!
  • dim(): how long AND how wide and object is; doesn’t work with vectors, only dataframes and matrices :(

9.5.1 is()

What is our “old.E.max” object?

is(old.E.max)
## [1] "numeric" "vector"
is(young.E.max)
## [1] "numeric" "vector"

Its a vector, containing numeric data.

What if we made a vector like this?

cat.variables <- c("old","old","old","old",
                   "old","old","old","old","old")

And used is()

is(cat.variables)
## [1] "character"           "vector"              "data.frameRowLabels"
## [4] "SuperClassMethod"

It tells us we have a vector, containing character data. Not sure why it feels the need to tell us all the other stuff…

9.5.2 length()

Our vector has 9 elements, or is 9 elements long.

length(old.E.max)
## [1] 9

Note that dim(), for dimension, doesn’t work with vectors!

dim(old.E.max)
## NULL

It would be nice if it said something like “1 x 9” for 1 row tall and 9 elements long. So it goes.

9.5.3 str()

str() stands for “structure”.

  • It summarizes info about an object;
  • I find it most useful for looking at lists.
  • If our vector here was really really long, str() would only show the first part of the vector
str(old.E.max)
##  num [1:9] 20.8 2.8 50 33.3 29.4 38.9 29.4 52.6 14.3

9.5.4 c()

  • We typically use c() to gather together things like numbers, as we did to make our objects above.
  • note: this is lower case “c”!
  • Uppercase is something else
  • For me, R’s font makes it hard sometimes to tell the difference between “c” and “C”
  • If code isn’t working, one problem might be a “C” instead of a “c”

Use c() to combine two objects

old.plus.new <- c(old.E.max, young.E.max)

Look at the length

length(old.plus.new)
## [1] 17

Note that str() just shows us the first few digits, not all 17

str(old.plus.new)
##  num [1:17] 20.8 2.8 50 33.3 29.4 38.9 29.4 52.6 14.3 45.5 ...

9.6 Debrief

We can…

  • learn about objects using length(), is(), str()
  • access parts of list using $ (and also brackets)
  • access parts of vectors using square brackets [ ]
  • save the output of a model / test to an object
  • access part of lists for plotting instead of copying stuff