##What is a vector? A vector is just a set of objects of the same type. You can create logical, character, numeric, complex or even factor vectors, among others. It is worth to mention that the different terms of the vector are called components. In addition, you can check the class of a vector with the class function and the type of the elements with the typeof function.

##Create vector in R Vectors in R can be created using the c function, that is used for object concatenation. You can save in memory a vector by assigning it a name with the <- operator.

# Creating R vectors with 'c' function
x <- c(12, 6, 67)
y <- c(2, 13)
y
## [1]  2 13

##Vectors can also be non-numeric. Hence, you can create vectors with characters, logical objects or other types of data objects.

state <- c("New York", "Ohio", "Washington", "Alabama")
class(state) # "character"
## [1] "character"
logic <- c(TRUE, TRUE, FALSE, TRUE)
class(logic) # "logical"
## [1] "logical"

##However, if you mix the data inside a vector the components will be coerced.

mix <- c(TRUE, "Correct", 8, 2.2)
mix # "TRUE" "Correct"  "8"  "2.2"
## [1] "TRUE"    "Correct" "8"       "2.2"
class(mix)  # "character"
## [1] "character"
typeof(mix) # "character"  
## [1] "character"

##Name vector in R ##You can also name vector elements. For that purpose just choose a name for each component or just for some of them.

my_vector <- c(orange = 4, apple = 6)
my_vector
## orange  apple 
##      4      6

##In addition, if you have already created the vector you can use the setNames function as follows:

setNames(y, c("Orange", "apple"))
## Orange  apple 
##      2     13
##Order vector in R
#Sort function
#For ordering or sorting a vector you can call the sort function passing the vector as argument. By default, the function sorts in ascending order.
z<-c(12,55,1,10)  
sort(z)
## [1]  1 10 12 55

##You can also sort data in decreasing order setting the decreasing argument to TRUE. Hence, we can call the following:

sort(z, decreasing = TRUE) 
## [1] 55 12 10  1

##Order function ##Alternatively, you can use brackets and order the vector components as an index making use of the order function.

##Ezoic # Increasing order z[order(z)] # Equivalent to sort(z)

Decreasing order

z[order(-z)] # Equivalent to sort(z, decreasing = TRUE) # Reversing the order of a vector

rev(z)
## [1] 10  1 55 12

##Combine vectors ##Combining two or more vectors is just easy as creating one. In fact, you just need to call the c function and pass the vectors as arguments, so you can add (append) a vector to other.

x <- c(1, 2, 3)
y <- c(4, 5, 6)
c(x, y)
## [1] 1 2 3 4 5 6

##It should be noted that the order of the components its relevant.

c(y, x)
## [1] 4 5 6 1 2 3

##Create empty vector ##Sometimes you need to initialize an empty vector in R and fill it within a loop. Whatever your needs, you can use the c function without specifying arguments. You could also use the vector function.

Empty vector

my_vector <- c()
my_vector
## NULL

Filling the vector using a for loop

for(i in 1:10) {
  my_vector[i] <- i
}

my_vector
##  [1]  1  2  3  4  5  6  7  8  9 10

Memory pre-allocation

my_vector <- rep(NA, 5)
my_vector <- vector(length = 5)

Filling the vector using a for loop

for(i in 1:5) {
  my_vector[i] <- i
  my_vector
}

##Compare two vectors ##There are several ways to compare vectors in R. Firstly, you can compare the elements one by one with some logical operator. Note that if one vector is greater than the other, the number of elements must be multiples or an error will occur.

x <- c(1, 5)
y <- c(4, 0)
x > y
## [1] FALSE  TRUE

FALSE TRUE

x <- c(1, 5)
y <- c(4, 0, 1, 3)

# This compares 1 > 4, 5 > 0, 1 > 1 y 5 > 3
x > y # FALSE TRUE FALSE TRUE
## [1] FALSE  TRUE FALSE  TRUE
x <- c(1, 5, 1)
y <- c(4, 0, 1, 3)
x > y # Error
## Warning in x > y: longer object length is not a multiple of shorter object
## length
## [1] FALSE  TRUE FALSE FALSE

##Secondly, you can also check if the elements of the first vector are contained in the second with %in%.

x %in% y # TRUE FALSE
## [1]  TRUE FALSE  TRUE

##Thirdly, another option is to return the common elements between the first vector and the second:

Return common elements

x[x %in% y] # 1
## [1] 1 1

Finally, you could compare if all the elements of the first vector are in the second with the function all as follows:

x <- c(1, 5)
y <- c(4, 5, 1, 3)

all(x %in% y) # TRUE
## [1] TRUE

##Sequence vectors in R ##In R, numeric sequences can be created in different ways. Among them, you can make use of the : operator or seq and rep functions.

Ezoic

1:4
## [1] 1 2 3 4

1 2 3 4

seq(1, 4, 0.5)
## [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0

1.0 1.5 2.0 2.5 3.0 3.5 4.0

seq(from = 1, to = 5, length.out = 9)
## [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0

1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0

rep(1, 5)
## [1] 1 1 1 1 1

1 1 1 1 1

##Generate random vector in R ##In R, there are several functions to deal with random number generation. The function sample allows you to create random sequences. In the following code we simulate 5 throws (sample size: 5) of a die (6 possible results).

sample(1:6, size = 5, replace = TRUE) 
## [1] 4 4 5 2 1

##The replace argument indicates whether the throw is with or without replacement. This means that if we set replace = FALSE and we get a 5 in the first throw, in the next throw we can only obtain 1, 2, 3, 4 or 6. ##You can also make use of the runif or rnorm functions, that generates random sequences of numbers through the Uniform and Normal distributions, respectively.

Normal values

rnorm(5, mean = 0, sd = 1)
## [1] -0.4391206 -2.0418522  0.8153583  0.2301354 -0.4556882
# -1.5611892 -0.2540665 -1.9912821 0.3040152 -1.4899171

Uniform values

runif(5, min = 2, max = 10)
## [1] 2.013851 6.186265 8.435143 8.629675 6.433160
# 8.929246 8.610883 7.686587 7.495158 3.771902

##Note on random number generation. When generating random numbers, you will obtain different values each time you execute the command, so your previous results will be different from ours. In order to set a random number generator seed to make a reproducible example you first need to call the set.seed function. Ezoic

set.seed(1) # You can set any other integer number as seed

Normal values

rnorm(5, mean = 0, sd = 1)
## [1] -0.6264538  0.1836433 -0.8356286  1.5952808  0.3295078
# -0.6264538  0.1836433  -0.8356286  1.5952808  0.3295078

Uniform values

runif(5, min = 2, max = 10)
## [1] 3.647797 3.412454 7.496183 5.072830 8.158731
# 3.647797 3.412454 7.496183 5.072830 8.158731

##Length of vector #You can get the length of a given vector with the length function. The length of a vector is the count of its components.

my_data <- c("vector", "sequence", "rnorm", "runif")

n <- length(my_data)
n
## [1] 4

Length of the vector

n # 4 ##Access elements of vectors in R ##Accessing index elements allows you to access unique elements like the first or the last elements, subset the vector, replace, change or delete some elements of a vector. You can achieve this with numeric or logical indices.

##Numeric index for accessing vector elements #In order to access the elements of a vector you can indicate inside brackets the corresponding vector subindex (positive integer).

#When you access to ‘negative’ positions it is understood that you want to access all positions less those positions. #Consider, for instance, the letters given by the letters function.

lett <- letters
lett
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
## [20] "t" "u" "v" "w" "x" "y" "z"

Ezoic # First element

lett[1]
## [1] "a"
# First element, simplifying the output class
lett[[1]]
## [1] "a"
# Third and fourth element
lett[c(3, 4)]
## [1] "c" "d"
# Last element of vector
lett[length(lett)]
## [1] "z"
# Even letters
lett[seq(2, n, 2)]
## [1] "b" "d"

Odd letters

lett[seq(1, n, 2)]
## [1] "a" "c"
lett[-seq(2, n, 2)] # Equivalent
##  [1] "a" "c" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u"
## [20] "v" "w" "x" "y" "z"

##Logical index for accessing vector elements ##Other possibility is to use a logical vector. In this case, you will access to the positions with TRUE value. Let’s see an example with the maximum monthly temperature of a Spanish city in 2017.

temp <- c(22.52, 18.70, 19.61, 22.79, 29.38, 30.19,
          33.16, 36.97, 33.29, 28.98, 24.31, 22.43)

month <- c("January", "February", "March", "April", "May", "June",
           "July", "August", "September", "October", "November", "December")

As an example, now you can look for the months with values greater than 30.

Ezoic # Months with maximum temperature greater than 30

month[temp > 30] 
## [1] "June"      "July"      "August"    "September"
month
##  [1] "January"   "February"  "March"     "April"     "May"       "June"     
##  [7] "July"      "August"    "September" "October"   "November"  "December"

Months with maximum temperature lower than 20 OR greater than 35

month[temp < 20 | temp > 35] 
## [1] "February" "March"    "August"
month
##  [1] "January"   "February"  "March"     "April"     "May"       "June"     
##  [7] "July"      "August"    "September" "October"   "November"  "December"

#Add element to R vector #Now you can try to add the ‘ñ’ letter to the vector we created before. First, you need to find the previous (or the following) letter in the alphabet. We will look for the ‘n’ letter and put the ‘ñ’ just after. You can make use of the which function to find the index of the element in the vector that corresponds with the letter ‘n’.

Looking for the index

n1 <- which(lett == "n")
n1 # 14
## [1] 14

#With this in mind, with a single line of code you can concatenate the characters.

Ezoic

c(lett[1:n1], "ñ", lett[-(1:n1)])
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "ñ" "o" "p" "q" "r"
## [20] "s" "t" "u" "v" "w" "x" "y" "z"
c
## function (...)  .Primitive("c")

#In case you want to add the element at the beginning or at the end of the vector just use the c function in the corresponding order.

Adding the letter ‘ñ’ at the beginning of the vector

c("ñ", lett)
##  [1] "ñ" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r"
## [20] "s" "t" "u" "v" "w" "x" "y" "z"
c
## function (...)  .Primitive("c")

Adding the letter ‘ñ’ at the end of the vector

c(lett, "ñ")
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
## [20] "t" "u" "v" "w" "x" "y" "z" "ñ"
c
## function (...)  .Primitive("c")

#How to delete a vector in R? #You can delete a vector in R with the rm function or assigning it other value, like NULL.

my_vector <- c(1, 2, 5, 6, 7)
my_vector
## [1] 1 2 5 6 7

With rm function

rm(my_vector)
rm
## function (..., list = character(), pos = -1, envir = as.environment(pos), 
##     inherits = FALSE) 
## {
##     if (...length()) {
##         dots <- match.call(expand.dots = FALSE)$...
##         if (!all(vapply(dots, function(x) is.symbol(x) || is.character(x), 
##             NA, USE.NAMES = FALSE))) 
##             stop("... must contain names or character strings")
##         list <- .Primitive("c")(list, vapply(dots, as.character, 
##             ""))
##     }
##     .Internal(remove(list, envir, inherits))
## }
## <bytecode: 0x000001c75adc4220>
## <environment: namespace:base>

Overriding the vector with other value

my_vector <- 0
my_vector
## [1] 0

Assigning NULL

my_vector <- NULL
my_vector
## NULL

#Delete value from vector #If you want to delete only some specific values of a vector you can use the - sign indicating the indexes you don’t want. Let’s see some examples.

vector <- c("London", "New York", "Paris")
vector
## [1] "London"   "New York" "Paris"

Deleting ‘London’

vector[-1] # "New York" "Paris"
## [1] "New York" "Paris"
vector[which(vector != "London")]  # Equivalent
## [1] "New York" "Paris"
vector[-which(vector == "London")] # Equivalent
## [1] "New York" "Paris"
vector
## [1] "London"   "New York" "Paris"

Tags:Data Structures Python Charts banner ################################END############################################