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 value5
to the variablex
. Note that<-
is the assignment operator in R. It assigns the value on the right to the variable, or object, on the left. Ifx
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 theprint
function to display the value ofx
. We’ll delve more into what functions look like shortly.
- Assignment statements, which assign values to variables. For example, the statement
- Expressions are blocks of code that, when evaluated, have some value. For example,
3 + 4
is an expression that evaluates to7
. Ifx
currently has the value of2
, then the expressionx > 1
evaluates toTRUE
.
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.
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
<- add_two_numbers(5, 9) sum_of_two_numbers
In the Functions section, there is much more detail about functions, and we will explain how to create your very own functions!
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: