1 Goal


The goal of this tutorial is to know the different types of vectors we can build in R.


2 Different data types

2.1 Numeric


# We put numerical values inside of the vector
my_numeric_vector <- c(5.4, 4.0, 2.3, 8)
str(my_numeric_vector)
##  num [1:4] 5.4 4 2.3 8

2.2 Integer


# We use now integer values
# Integer values are positive integer values
my_integer_vector <- as.integer((c(5, 7, 9, 1, 12)))
str(my_integer_vector)
##  int [1:5] 5 7 9 1 12

2.3 Character


# We can add character values into a vector
my_character_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
str(my_character_vector)
##  chr [1:7] "Monday" "Tuesday" "Wednesday" "Thursday" ...

2.4 Logical (Boolean)


# Logical variables are defined as TRUE or FALSE
my_logical_vector <- c(TRUE, TRUE, FALSE, TRUE, FALSE)
str(my_logical_vector)
##  logi [1:5] TRUE TRUE FALSE TRUE FALSE
# We could as well build the same vector doing
my_logical_vector <- as.logical(c(1, 1, 0, 1, 0))
str(my_logical_vector)
##  logi [1:5] TRUE TRUE FALSE TRUE FALSE

2.5 Complex


# Complex numbers are formed by two components
my_complex_number <- c(3 + 4i, 2 + 1i, 10 + 6i, 6 + 3i)
str(my_complex_number)
##  cplx [1:4] 3+4i 2+1i 10+6i ...

3 Conclusion


In this tutorial we have learnt the difference between the 5 types of variables that we can use to create a vector.