Data types and working with vectors

Author

Amanda Ng, Abigail Frix

Data types

In R, we can store different types of data, such as doubles (numeric values), integers, logical values (TRUE/FALSE), and characters (text). To check the type of an object, we use the typeof() function.

To create character variables in R, we enclose the text in quotation marks, like “…”.

The most common data type in R is a vector, which is a series of values composed of either numbers or characters. We can create our own vectors by either typing all values in manually or using built in commands.

Sequencing

seq can be used to automate this process if you have a constant change between values.

The by and length.out arguments in the seq() function control how the sequence is generated:

  • by sets the constant step size between each number in the sequence.
  • length.out sets the total number of elements in the sequence and adjusts the step size automatically.

For example, if you want to create a variable for time for a timeseries over 25 years, it could look like this:

To create a sequence with different increments:

To create a sequence with a fixed number of timestamps.

You can explore certain qualities of a vector using built in functions. Some of these are:

These functions are useful when you are working with large datasets and are performing exploratory analysis to see what types of visualizations and analysis you can perform on your data. But more on that later!

Sometimes you only want to extract certain values from a given vector. You can subset vectors to extract certain values using certain strategies:

Indexing the vector

In R, you can access specific elements of a vector using square brackets [ ] and specifying the position of the element. Indexing starts at 1 (unlike 0 in some other programming languages).

Using conditions and logical statements

Logical comparisons compare values and return TRUE or FALSE. Below is a list of common comparison operators:

  • >: Strictly greater than
  • >=: Greater than or equal to
  • <: Strictly less than
  • <=: Less than or equal to
  • ==: Equal to (Note: double-equal-signs are used to avoid confusion with =, which is sometimes also used as an assignment function similar to <-)
  • !=: Not equal to (Note: ! represents NOT)
  • is.na(): Value is NA

Here is an example:

Handling missing data

Missing data isn’t uncommon when working with real world data, and R has built in functions to remove it (as the presence of NA values can lead to error in calculations). For example:

So, to fix this we can remove the missing values a few different ways:

For loops

In simple terms, a for loop functions by performing a certain operation for each number in a list. For loops are generally not used as much in R as in some other programming languages, but there are instances where they might be the most efficient method in dealing with certain types of data. For now, let’s demonstrate the concept using an earlier example of a function:

Let’s do another example. Say we want to make a for loop to calculate the incidence of influenza in a hypothetical population.

In these examples, there are often simpler built in functions in R to get the same results. These were merely examples to demonstrate how a for loop functions: apply this command to every item in a vector. Examples where for loops are used regularly in epidemiology is for modeling the spread of an infectious disease in a susceptible population. We won’t delve into these concepts as they go into detail beyond the scope of the course, but it is important to understand the basics of a for loop for future reference!

Return to Learning Hub Homepage

Learning Hub Homepage