Quick intro to R syntax

Syntax

Basics

R code is composed of statements and expressions:

  • Statements perform actions. The most common statements would be:
    • Assignment statements, which assign values to variables. For example, the statement x <- 5 assigns the value 5 to the variable x. Note that <- is the assignment operator in R. It assigns the value on the right to the variable, or object, on the left. If x already had a value, that value will be replaced, or overwritten, by the new value.
    • Function calls, which execute functions. For example, the statement print(x) calls the print function to display the value of x. We’ll delve more into what functions look like shortly.
  • Expressions are blocks of code that, when evaluated, have some value. For example, 3 + 4 is an expression that evaluates to 7. If x currently has the value of 2, then the expression x > 1 evaluates to TRUE.

Expressions are often embedded in assignment statements, such as in the assignment statement y <- 3 + 4. When R sees an assignment statement, first it evaluates the right side, then it assigns that result to the variable named on the left. So in this case, R would evaluate the expression 3 + 4 to get 7, then it would assign that value to the variable y.

Variables (a.k.a. objects)

Variables allow us to use names (such as x or patient_data) to refer to data that is stored in our R environment. The data that a variable or object name refers to can be of many different types, such as an individual number, a sequence of text values, a matrix of numbers, or a complicated structure that holds multiple types of data.

Variable names can contain letters, numbers, periods, and underscores. They must start with a letter or a period (but not a period followed by a number). Variable names are case-sensitive, so year and Year would be considered different variables.

Comments

Comments are lines of text that are not executed as code. You can use them to explain and document the code, making it easier to understand. It can also be handy to “comment out” a line of code in order to temporarily disable it.

In R, comments start with the # symbol. Everything on a line after the # is considered a comment and is ignored by R when the code is run. So the entire line might start with #, or there might be code first and then a comment at the end of the line starting with #. For example:

# This is a comment, but the next line is code that will run
x <- 5

y <- x + 2  # The code to the left will run, but everything after the # is a comment and will not be run

Functions

We tend to use a lot of functions in R. Functions are blocks of code that perform specific tasks. They take inputs (called arguments or parameters), process them, and usually* return an output (* some functions, by design, don’t return an output). A function might be used like this:

# Call add_two_numbers and assign the result to sum_of_two_numbers
sum_of_two_numbers <- add_two_numbers(5, 9)

In the Functions section, there is much more detail about functions, and we will explain how to create your very own functions!