R Programming works with numerous data types, including
Basics types
We can check the type of a variable with the class function
Variables store values and are an important component in programming, especially for a data scientist. A variable can store a number, an object, a statistical result, vector, dataset, a model prediction basically anything R outputs. We can use that variable later simply by calling the name of the variable.
To declare a variable, we need to assign a variable name. The name should not have space. We can use _ to connect to words.
To add a value to the variable, use <- or =.
Here is the syntax:
# First way to declare a variable: use the `<-`
name_of_variable <- value
# Second way to declare a variable: use the `=`
name_of_variable = value
In the command line, we can write the following codes to see what happens:
A vector is a one-dimensional array. We can create a vector with all the basic data type we learnt before. The simplest way to build a vector in R, is to use the c command.
We can do arithmetic calculations on vectors.
# Create the vectors
vect_1 <- c(1, 3, 5)
vect_2 <- c(2, 4, 6)
# Take the sum of A_vector and B_vector
sum_vect <- vect_1 + vect_2
# Print out total_vector
sum_vect
## [1] 3 7 11
In R, it is possible to slice a vector. In some occasion, we are interested in only the first five rows of a vector. We can use the [1:5]
command to extract the value 1 to 5.
# Slice the first five rows of the vector
slice_vector <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
slice_vector[1:5]
## [1] 1 2 3 4 5
We will first see the basic arithmetic operations in R. The following operators stand for:
Operator | Description |
---|---|
|
Addition |
|
Subtraction |
|
Multiplication |
/ | Division |
^ or ** | Exponentiation |
You can easily copy and paste the above R code into Rstudio Console. The output is displayed after the character #. For instance, we write the code print(‘Hello world’) the output will be ##[1] “Hello world”.
The ## means we print an output and the number in the square bracket ([1]) is the number of the display
The sentences starting with # annotation. We can use # inside an R script to add any comment we want. R won’t read it during the running time.
With logical operators, we want to return values inside the vector based on logical conditions. Following is a detailed list of logical operators available in R
Operator | Description |
---|---|
< | Less than |
<= | Less than or equal to |
> | Greater than |
>= | Greater than or equal to |
== | Exactly equal to |
!= | Not equal to |
!x | Not x |
x & y | x AND y |
isTRUE(x | Test if x is TRUE |
The logical statements in R are wrapped inside the [ ]
. We can add many conditional statements as we like but we need to include them in a parenthesis. We can follow this structure to create a conditional statement:
With variable_name referring to the variable, we want to use for the statement. We create the logical statement i.e. variable_name > 0
. Finally, we use the square bracket to finalize the logical statement. Below, an example of a logical statement.
## [1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE
In the output above, R reads each value and compares it to the statement logical_vector > 5
. If the value is strictly superior to five, then the condition is TRUE, otherwise FALSE. R returns a vector of TRUE and FALSE.
In the example below, we want to extract the values that only meet the condition ‘is strictly superior to five’. For that, we can wrap the condition inside a square bracket precede by the vector containing the values.
## [1] 6 7 8 9 10
A work by Gianluca Sottile
gianluca.sottile@unipa.it