Intro to Variables and Dataframes

We start with a brief recap of what we learned in the Intro to R. After that, we will learn about and work with different types of variables, vectors, matrices and data frames. Subsequently, we will focus on putting our new knowledge into practice by creating our own toy data frame.

0) Learning Objectives:

Enable you to understand, manipulate and create:

  1. variables;
  2. vectors;
  3. matrices;
  4. data frames.

Table of contents:

  1. Introduction to concepts: 1 hour
  2. Workshop: 5 hours
  3. Mock-assessment: 1 hour

Questions or issues?

If you have any questions or issues regarding the course material, please first ask your peers or ask us in the daily Q&A at 16:00!

Good luck!

1) Introduction

1.1 Variables

Let's start with the most basic container for information: the variable. Essentially it's just a container that contains a value that can change depending on a given condition. The following video covers the fundamental concepts of variables from a mathematical perspective.

The next video zooms into variables in the context of data & computer science; more specifically: how variables work in R.

1.2 vectors

Whereas a variable stores a single value, a vector stores multiple values of the same kind in one dimension.

1.2 Matrices

Matrices store multiple (usually numeric) values of the same type in two dimensions arranged into a fixed number of rows and columns. So every row, or observation or element, contains a value for every collum.

The singular form of matrices is ‘matrix'.

Enter the (data in the) matrix

So it's essentially a matrix is a table with values of the same type. It's exciting stuff.

1.4 Dataframes

And finally, there's the data frame which is similar to the matrix but can store different types of data in two dimensions.

2) Workshop - Variables, Vectors and Dataframes

Install the following course and do modules 1, 4, 6 and 7.

  > swirl::install_course("R Programming")

If you have any questions, first, ask your peers before you ask us or notify us of any issues or questions you're having.

3) Mock Assessment

This mock assessment is about applying the concepts we learned today to a real-world situation. Today we're interested in your day, what did you have for lunch and breakfast for example? You can also come up with some food or drink items instead if, for example, you didn't eat anything yet. Sounds straightforward, right? So let's get started!

Create an R-script called "VariablesMockAssessment" and follow these instructions:

  1. Create a variable called ‘lunch' with a string describing an item you had for lunch: food only.
  2. Delete the variable.
  3. Create a vector named ‘drinks' containing all your drinks you ate today: as strings.
  4. Create a vector named ‘foods' listing all the food you ate today; as strings.
  5. Create a matrix containing the food and drinks you had today.
  6. Create a data frame from the matrix and add the times; as numerical values, when you ate or drank something.

Save your mock assessment to your GitHub repository. In Datalab you and your classmates are going to look at one another's scripts and run it.

4) Daily Q&A

At 16:00, there's a online meeting on our Microsoft Team Channel you're encouraged to take part in to ask questions and to discuss our progress and reflect on today activities.

Tomorrow we will cover descriptive analysis: what can we tell about the data by just looking at the data distribution and summary statistics?

Resources