

This is indicated by the 3 at the beginning of the output, and the 2 at the end of the output. Order() gives the permutation to apply to the vector in order to sort its elements: order(x) # 3 4 5 1 2Īs you can see, the third element of the vector is the smallest and the second element is the largest. We can sort the elements of a vector from smallest to largest, or from largest to smallest: x <- c(2.1, 5, -4, 1, 1) To transform a character vector to uppercase and lowercase: toupper(c("Rafael Nadal", "Roger Federer", "Novak Djokovic")) # "RAFAEL NADAL" "ROGER FEDERER" "NOVAK DJOKOVIC" tolower(c("Rafael Nadal", "Roger Federer", "Novak Djokovic")) # "rafael nadal" "roger federer" "novak djokovic" Split a character string based on a specific symbol with the strsplit() function: strsplit(c("Rafael Nadal", "Roger Federer", "Novak Djokovic"), Replacement = "BEL", # replace it with BEL Replace a character string by another one if it exists in the vector by using the sub() function: sub( To extract a character string based on the beginning and the end positions, we can use the substr() function: substr(code,

To find the positions of the elements containing a given string, use the grep() function: grep("BE", code) # 1 2 5

The argument sep stands for separator and allows to specify the character(s) or symbol(s) used to separate each character strings. If you need to round a number, you can use the round(), floor() and ceiling() functions: round(cos(x), digits = 3) # 3 decimals # -0.505 0.284 -0.654 0.540 floor(cos(x)) # largest integer not greater than x # -1 0 -1 0 ceiling(cos(x)) # smallest integer not less than x # 0 1 0 1 It is also possible to compute the minimum, maximum, sum, product, cumulative sum and cumulative product of a vector: min(x) # -4 max(x) # 5 sum(x) # 4.1 prod(x) # -42 cumsum(x) # 2.1 7.1 3.1 4.1 cumprod(x) # 2.1 10.5 -42.0 -42.0 The basic numerical operators such as +, -, *, / and ^ can be applied to vectors: x <- c(2.1, 5, -4, 1) Add new variables from another data frame.Add new observations from another data frame.

