In this tutorial, you will learn:
Following are the Data Types or Data Structures in R Programming:
- Vectors (numerical, character, logical)
- Data frames
- 4.5 is a decimal value called numerics.
- 4 is a natural value called integers. Integers are also numerics.
- TRUE or FALSE is a Boolean value called logical binary operators in R.
- The value inside ” ” or ‘ ‘ are text (string). They are called characters.
We can check the type of a variable with the class function
# Declare variables of different types # Numeric x <- 28 class(x)
##  "numeric"
# String y <- "R is Fantastic" class(y)
##  "character"
# Boolean z <- TRUE class(z)
##  "logical"
Variables are one of the basic data types in R that store values and are an important component in R programming, especially for a data scientist. A variable in R data types 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 variable data structures in R, 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 in data types in R programming, 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:
# Print variable x x <- 42 x
##  42
y <- 10 y
##  10
# We call x and y and apply a subtraction x-y
##  32
A vector is a one-dimensional array. We can create a vector with all the basic R data types we learnt before. The simplest way to build vector data structures in R, is to use the c command.
# Numerical vec_num <- c(1, 10, 49) vec_num
##  1 10 49
# Character vec_chr <- c("a", "b", "c") vec_chr
##  "a" "b" "c"
# Boolean vec_bool <- c(TRUE, FALSE, TRUE) vec_bool
## TRUE FALSE TRUE
We can do arithmetic calculations on vector binary operators in R.
# 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
 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 2 3 4 5
The shortest way to create a range of values is to use the: between two numbers. For instance, from the above example, we can write c(1:10) to create a vector of value from one to ten.
# Faster way to create adjacent values c(1:10)
##  1 2 3 4 5 6 7 8 9 10
We will first see the basic arithmetic operators in R data types. Following are the arithmetic and boolean operators in R programming which stand for:
|^ or **||Exponentiation|
# An addition 3 + 4
##  7
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(‘Guru99’) the output will be ## Guru99.
The ## means we print output and the number in the square bracket () 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.
# A multiplication 3*5
##  15
# A division (5+5)/2
##  5
# Exponentiation 2^5
##  32
# Modulo 28%%6
##  4
With logical operators, we want to return values inside the vector based on logical conditions. Following is a detailed list of logical operators of data types in R programming
The logical statements in R are wrapped inside the . We can add as 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.
# Create a vector from 1 to 10 logical_vector <- c(1:10) logical_vector>5
## 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.
# Print value strictly above 5 logical_vector[(logical_vector>5)]
##  6 7 8 9 10
# Print 5 and 6 logical_vector <- c(1:10) logical_vector[(logical_vector>4) & (logical_vector<7)]
##  5 6