Course: Data-Driven Management and Policy

Prof. José Manuel Magallanes, PhD


Session 3: Data Structures

Before starting, keep in mind the following ideas:

We are going to talk about 3 data structures in R:

  1. Lists.
  2. Vectors.
  3. Data Frame.

Lists and vectors are simple structures; a data frame is a more complex one (built from the simple ones).


List

Lists are containers of values. The values can be of any kind (numbers or non-numbers), and even other containers (simple or complex).

If we have an spreadsheet as a reference, a row is a ‘natural’ list.

Then this can be a list:

DetailStudent=list("Fred Meyers",
                   40,
                   FALSE)

The object DetailStudent serves to store temporarily the list in the computer. To name a list, use combinations of letters and numbers in a meaningful way (do not start with a number or a special character).

Typing the name of the object DetailStudent, now representing a list, will give you all the contents you saved in there:

DetailStudent

The list above has three elements. However, you may be wondering if those elements have a meaning altogether. In those situations, it is better to have names for each elements.

DetailStudent=list(fullName="Fred Meyers",
                   age=40,
                   female=FALSE)
# seeing the result
DetailStudent

This list has three elements, which we can also call fields. Each of these, in this case, holds a different data type:

  • FullName holds characters
  • age holds a number
  • female holds a logical (Boolean) value.

You can access any of those elements using these approaches:

# position
DetailStudent[[1]]
# name of the field
DetailStudent[['fullName']]
# name of the field
DetailStudent$fullName

If you do not have names for the list fields, you can only access them using positions:

NewList=list('a','b','c','d',1,2,3)
NewList[[1]]

Once you access an element, you can alter it:

DetailStudent[[1]]='Alfred Mayer'
# Then:
DetailStudent

You can even add an totally NEW field like this:

DetailStudent$city='Seattle'

# show:
DetailStudent

And destroy it by NULLing it, like this:

DetailStudent$city=NULL # do you like: DetailStudent[[4]]=NULL
DetailStudent

You can get rid of a list using:

rm(DetailStudent)
DetailStudent

** How would you create a list of this person out of his personal information data?**