Getting Started

Author

Jeremy Selva

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.

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.

TipFully worked solution:
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 <- 20

In 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.

https://resbaz.github.io/2014-r-materials/lessons/01-intro_r/data-structures.html