How Do We Code in R?

Look into basic R programming concepts to help us get started with R.

While it’s not required to be a seasoned coder/computer programmer to use R, there is still a set of basic programming concepts that new R users need to understand. Even though this isn’t a programming course, we’ll still learn just enough of these basic programming concepts needed to explore and analyze data effectively.

Press + to interact

Basic programming concepts and terminology

Now, we’ll learn some basic programming concepts and terminology. Instead of memorizing all these concepts and terminology right now, the best way to master these topics is through deliberate practice with R, and lots of repetition.

Basics

  • Console pane: We enter the commands here.

  • Running code: We tell R to perform an act by giving it commands in the console.

  • Objects: We use objects to save values in R. We’ll show how to assign values to objects and how to display the contents of objects.

  • Data types: We have integers, doubles/numerics, logical, and characters.

    • Integers are values like -1, 0, 2, 4092.

    • Doubles or numerics are a larger set of values containing both the integers but also fractions and decimal values like -24.932 and 0.8.

    • Logicals are either TRUE or FALSE.

    • Characters are text such as “cabbage,” “Hamilton,” “The Wire is an entertaining TV show,” and “This ramen is delicious.” We should note that characters are often denoted with quotation marks around them.

  • Vectors: These are a series of values created using the c() function, where c() stands for combine or concatenate. For example, c(6, 11, 13, 31, 90, 92) creates a six-element series of positive integer values.

  • Factors: We use this to represent categorical data in R. Categorical data can also be represented as strings. We’ll study this difference as we progress through the course.

  • Data frames: These are rectangular spreadsheets. They are representations of datasets in R where the rows correspond to observations and the columns correspond to variables that describe the observations.

Conditionals

Let’s explore how conditionals work in R:

  • Boolean algebra: These include TRUE/FALSE statements and mathematical operators such as < (less than), <= (less than or equal), and != (not equal to).

    • For example, 4 + 2 >= 3 will return TRUE but 3 + 5 <= 1 will return FALSE.

    • We test for equality in R using == (and not =, which is typically used for assignments). For example, 2 + 1 == 3 compares 2 + 1 to 3 and is the correct R code while 2 + 1 = 3 will return an error.

  • Logical operators: This includes & representing “and” as well as | representing “or.” For example, (2 + 1 == 3) & (2 + 1 == 4) returns FALSE because both clauses aren’t TRUE (only the first clause is TRUE). On the other hand, (2 + 1 == 3) | (2 + 1 == 4) returns TRUE because at least one of the two clauses is TRUE.

Press + to interact
# Test if 2 + 1 is equal to 3
if (2 + 1 == 3) {
# If the condition is TRUE, print the message
print("2 + 1 is equal to 3")
} else if (2 + 1 > 3) {
# If the previous condition is FALSE and this condition is TRUE, print the message
print("2 + 1 is greater than 3")
} else {
# If all previous conditions are FALSE, print the message
print("2 + 1 is not equal to or greater than 3")
}
# Test if 2 + 1 is equal to 3 AND 2 + 1 is less than 5
if ((2 + 1 == 3) & (2 + 1 < 5)) {
# If both conditions are TRUE, print the message
print("2 + 1 is equal to 3 and less than 5")
} else {
# If one or both conditions are FALSE, print the message
print("2 + 1 is not equal to 3 or greater than or equal to 5")
}

Functions are also called commands

Functions perform tasks in R, and they take in inputs called arguments and return outputs. We can either manually specify a function’s arguments or use the function’s default values.

  • For example, the function seq() in R generates a sequence of numbers. If we just run seq(), it’ll return the value 1. That doesn’t seem very useful! This is because the default arguments are set as seq(from = 1 to = 1). Therefore, if we don’t pass in different values for from and to, R just assumes that all we want is the number 1. To change this behavior, we can update the argument values after the = sign. If we try out seq(from = 2 to = 5), we get the result 2 3 4 5 that we might expect.

Note: We’ll work with functions a lot throughout this course and get lots of practice in understanding their behaviors. To further assist in understanding when a function is mentioned in the course, we’ll include the () after it as we did with seq() above.

Press + to interact
# Generate a sequence of numbers from 1 to 5
sequence <- seq(from = 1, to = 5)
# Print the sequence
print(sequence)
# Generate a sequence of numbers from 1 to 10 by 2
sequence <- seq(from = 1, to = 10, by = 2)
# Print the sequence
print(sequence)

Errors, warnings, and messages

One thing that intimidates new R users is how it reports errors, warnings, and messages. R reports errors, warnings, and messages in a red font, which makes it seem like it’s scolding us. However, seeing red text in the console isn’t always bad.

R will show red text in the console pane in three different situations:

  • Errors: When the red text is a legitimate error, it’ll be prefaced with “Error in...” and will try to explain what went wrong. Generally, when there’s an error, the code won’t run.

  • Warnings: When the red text is a warning, it’ll be prefaced with “Warning:” and R will try to explain why there’s a warning. Generally, our code will still work, but with some caution.

  • Messages: When the red text doesn’t start with either “Error” or “Warning,” it’s just a friendly message. We’ll see these messages when we load R packages or read data saved in spreadsheet files with the read_csv() function. These are helpful diagnostic messages that won’t stop our code from working.

This list is by no means an exhaustive list of all the programming concepts and terminology needed to become a savvy R user. Such a list will be so large that it won’t be very useful, especially for beginners. Rather, we feel that this is a minimally viable list of programming concepts and terminology we need to know before getting started. Remember that our mastery of all of these concepts and terminology will build as we practice more and more.

Also remember, when we see red text in the console, don’t panic, because it doesn’t necessarily mean anything is wrong.