Getting Started
Introduction to R
R is a programming language that was initially designed for statistical analysis. It later got popular among data scientists. In practice, most data science teams use a mix of programming languages, often at least R and Python. Nevertheless, it is better to master one tool at a time and R is a great place to start. Keep in mind that R is not just software. There are many wonderful open communities with contributors all working to make R easier to learn and more enjoyable to use.
Like learning a new language (spoken or computer), the initial learning curve may be steep. We hope that this section can help you climb the initial learning curve and provide you with the basic skills and self-confidence to further your experience in using R.
Learning Goals
- Use functions from R.
- Understand objects from R.
- Know the different data types in R.
- Perform comparisons in R
Functions
Let’s take a look on this first line of code. Click on the button Run Code to see the output.
You will see that Hello R 😃 is printed to the screen.
In this example, print() is a function, and its job is to print whatever text we place in between the double quotes ". Try putting a different text in this exercise below.
print("Well done ! 👏")Function names are usually action words represented with an open and close parentheses () in the following form.
bake(cake)In the above example, bake() is the function and the input cake is formally called an argument of the function.
It is possible for a function to accept more than one arguments or no arguments at all.
bake(cake, banana, chocolate)
bake()Here is an example of a function Sys.time() which does not need an argument. It gives the current date, time and time zones value.
Objects
Most things in R are an object. An R object can be almost anything, from a single number, symbol or character string (like a word) to a highly complex structure like a plot or dataset. We can create an object and assign values to the created object. A created object is sometimes called a variable.
Consider the line of code below,
number_of_patients_in_ward_A <- 20In this example, both <- and 20 are objects that R provides. The variable number_of_patients_in_ward_A is the object created. We assign the value 20 to number_of_patients_in_ward_A using the symbol <-. In R, the symbol <- is called an object assignment operator. It comprised of a “less than” symbol < and a hyphen -.
As a result, number_of_patients_in_ward_A holds the value of 20, which R then stored it into memory.
Since number_of_patients_in_ward_A is stored in memory as a number, we can perform different operations such as adding and removing patients.
Data Types
Atomic Classes
We share a few common atomic classes in R.
- logical (
TRUE,FALSE) - numeric (real or decimal) (
2,2.0,pi) - integer (
2L,as.integer(3)) - character (
"a","swc")
They can be identified using the function class(). Below are some examples that you can try it out.
To assign an explicit integer, we add an L beside the number.
Data Structure
R also has several common data structures.
- vector
- factors (1st, 2nd, 3rd, …)
- data frame
A vector is an one-dimensional data structure of the same atomic class (logical, numeric, character, …). To create a vector in R, we use the function c() works by combining objects together. Below are some examples done to create ward_names as a character vector and number_of_patients as a numeric vector.
We can check if a given variable is a vector using the function is.vector. In addition, class() can be used to identify which atomic class the vector contains.
Factors are special kind of vectors that represent categorical data. They can be created using the factor() function. The most common input is a character vector. It also has a parameter called levels that accepts an optional vector of the unique values that tells R if the category has an order to follow.
We can check if a given variable is a factor using the function is.factor() or class().
We can build data frame from the vectors created earlier using the function, data.frame().
We can check if a given variable is a data frame using the function is.data.frame(). str() helps to provide the atomic class information for each column in the data frame.
Operators
Consider the number of patients in different wards.
Comparison Operators
R has operators (>, >=, <, <=, ==, !=) to compare the relationship between two values. They usually returns TRUE or FALSE.
The > operator means greater than.
The >= operator means greater than or equal to.
The < operator means less than.
The <= operator means less than or equal to.
The == operator means equal to.
The != operator means not equal to.
These comparison operators can be used to compare each element in a vector individually.
Logical Operators
R has logical operators (&, |, !, is.na(), isTRUE(), isFALSE(), %in%, %notin%) to compare multiple operation. They usually returns TRUE or FALSE.
The & operator means logical AND which returns TRUE if both conditions are true.
The | operator means logical OR which returns TRUE if at least one of the conditions is true.
The ! operator means logical NOT which returns TRUE if the condition is false.
The is.na function returns TRUE if a value is NA.
The isTRUE function returns TRUE for TRUE elements, FALSE for FALSE elements. Please note that this function only works in length one logical vectors. To apply this check element wise on a logical vector greater than 1, one work around is to use Vectorize.
The isFALSE function returns FALSE for TRUE elements, TRUE for FALSE elements.
The %in% operator checks if a value or set of values on the left side is present in a vector or list on the right side.
The %notin% operator was introduced in R version 4.6.0. It checks if a value or set of values on the left side is absent in a vector or list on the right side.