Basic numerical commands
- Assign a value:
x = 4 #Assign a value
x
## [1] 4
x = 3 ; x = 5 #the value of the variable is the last that has been assigned. ";" separetes different steps.
x
## [1] 5
- Sequences:
x = 1:10 #Sequence from 1 to 10
x
## [1] 1 2 3 4 5 6 7 8 9 10
vec = c(1,2,3,4,5) # c(): function that combines its arguments.
vec
## [1] 1 2 3 4 5
x = 1:3;y = 3:5;z = 5:7
vec1 = c(x,y,z) #c() combines the values of x,y,z
vec1
## [1] 1 2 3 3 4 5 5 6 7
rep()
function:
rep()
3 arguments: an object (vector, factor, etc) to be repeated times
: number of times to repeat each object lenght.out
: lenght of the output vector each
: times each element of the object is repeatedx = 1:5
rep(x, 2) # 2 times the x sequence
## [1] 1 2 3 4 5 1 2 3 4 5
rep(x, each = 2) # 2 times each value in the seq
## [1] 1 1 2 2 3 3 4 4 5 5
rep(x, c(1,2,1,2,1)) # every value is repeated the times stablished in the vector
## [1] 1 2 2 3 4 4 5
rep(x, each = 3, lenght.out = 5) # lenght = 5, lenght.out can be abbreviated to `len` or `length`
## [1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
rep(x, each = 3, times = 2) # 2 times the seq x, with each value of the seq x repeted 3 times
## [1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
rep(x, times = 2, lenght.out= 6 , each = 1) # lenght = 6
## [1] 1 2 3 4 5 1 2 3 4 5
#lenght.out can be abbreviated to `len` or `length`:
rep(x, times = 2, len= 6 , each = 1)
## [1] 1 2 3 4 5 1
seq()
function:
seq() has the following parameters:
from
, to
: starting and end values by
: increment of the sequence lenght.out
: lenght of the sequenceseq(13) #1:13
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13
seq(from = 1, to = 10, by = 2) #from 1 to 10 by 2
## [1] 1 3 5 7 9
seq(1,10, by = 0.5) #from 1 to 10 by 0.5
## [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5
## [15] 8.0 8.5 9.0 9.5 10.0
seq(0, 1, length.out = 5) #from 1 to 0, didvided by 5
## [1] 0.00 0.25 0.50 0.75 1.00
##lenght.out can be abbreviated to `len` or `length`:
seq(0, 1, len = 5) #from 1 to 0, didvided by 5
## [1] 0.00 0.25 0.50 0.75 1.00
seq(0, 1, length = 5)
## [1] 0.00 0.25 0.50 0.75 1.00
seq(10, 19, by = 2) #non exact end, stays below end
## [1] 10 12 14 16 18
seq(10, 19, by = 3) #exact end
## [1] 10 13 16 19
seq(1, 9, by = pi)
## [1] 1.000000 4.141593 7.283185
paste()
andpaste0()
functions:
paste()
has 3 arguments: the values to be paste sep
: string to separate the terms. collapse
: optional string to separate the resultspaste(1:5, c("a","b","c","d","e"), sep = "-", collapse= NULL)
## [1] "1-a" "2-b" "3-c" "4-d" "5-e"
paste(1:5, c("a","b","c","d","e"), sep = "-", collapse= 'Hi')
## [1] "1-aHi2-bHi3-cHi4-dHi5-e"
paste0()
does the same aspaste()
function but has notsep
argument,paste0()
is equivalent apaste(..., sep="")
paste(1:5, c("a","b","c","d","e"), sep = "", collapse= NULL)
## [1] "1a" "2b" "3c" "4d" "5e"
paste0(1:5, c("a","b","c","d","e"))
## [1] "1a" "2b" "3c" "4d" "5e"
Matrix and data frame
Both of them are ways to store information. The difference between them is that in the matrix all the data has the same class (numerical, character, logical, etc) while in a data.frame they can be different.
mat = matrix(1:100, nrow = 10, ncol = 10, byrow = FALSE)
mat
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 1 11 21 31 41 51 61 71 81 91
## [2,] 2 12 22 32 42 52 62 72 82 92
## [3,] 3 13 23 33 43 53 63 73 83 93
## [4,] 4 14 24 34 44 54 64 74 84 94
## [5,] 5 15 25 35 45 55 65 75 85 95
## [6,] 6 16 26 36 46 56 66 76 86 96
## [7,] 7 17 27 37 47 57 67 77 87 97
## [8,] 8 18 28 38 48 58 68 78 88 98
## [9,] 9 19 29 39 49 59 69 79 89 99
## [10,] 10 20 30 40 50 60 70 80 90 100
mat1 = matrix(1:100, 10, 10, byrow = TRUE, dimnames = list(c(LETTERS[1:10]), c(paste("Col",1:10))))
mat1
## Col 1 Col 2 Col 3 Col 4 Col 5 Col 6 Col 7 Col 8 Col 9 Col 10
## A 1 2 3 4 5 6 7 8 9 10
## B 11 12 13 14 15 16 17 18 19 20
## C 21 22 23 24 25 26 27 28 29 30
## D 31 32 33 34 35 36 37 38 39 40
## E 41 42 43 44 45 46 47 48 49 50
## F 51 52 53 54 55 56 57 58 59 60
## G 61 62 63 64 65 66 67 68 69 70
## H 71 72 73 74 75 76 77 78 79 80
## I 81 82 83 84 85 86 87 88 89 90
## J 91 92 93 94 95 96 97 98 99 100
class(mat1)
## [1] "matrix"
datfra = data.frame(Numbers = 1:11, let1 = letters[10:20], let2 = LETTERS[15:25], Numagain = 34:44)
datfra
## Numbers let1 let2 Numagain
## 1 1 j O 34
## 2 2 k P 35
## 3 3 l Q 36
## 4 4 m R 37
## 5 5 n S 38
## 6 6 o T 39
## 7 7 p U 40
## 8 8 q V 41
## 9 9 r W 42
## 10 10 s X 43
## 11 11 t Y 44
datfra$let1
## [1] j k l m n o p q r s t
## Levels: j k l m n o p q r s t
datfra$Numagain
## [1] 34 35 36 37 38 39 40 41 42 43 44
class(datfra)
## [1] "data.frame"
Different classes in R
class(x) #function `class()` allows us to know which class is the object we are working with
## [1] "integer"
x = 1:5 ; class(x)
## [1] "integer"
x = 1.4567 ; class(x)
## [1] "numeric"
x = data.frame(1:10) ; class(x)
## [1] "data.frame"
x = matrix(1:10) ; class(x)
## [1] "matrix"
x = "classes" ; class(x)
## [1] "character"
x = c("One", "Two", "Three"); class(x)
## [1] "character"
x = lm ; class(x)
## [1] "function"
#the following functions allow us to know if an object is a specific class or not.
is.integer(x)
## [1] FALSE
is.numeric(x)
## [1] FALSE
is.character(x)
## [1] FALSE
is.data.frame(x)
## [1] FALSE
is.matrix(x)
## [1] FALSE
is.vector(x)
## [1] FALSE
#the following options allows us to modify the class of an object
x = c(1:5)
as.character(x)
## [1] "1" "2" "3" "4" "5"
as.numeric(x)
## [1] 1 2 3 4 5
y = c("hello", "how", "are", "you")
as.numeric(y)
## Warning: NAs introduced by coercion
## [1] NA NA NA NA
as.matrix(x)
## [,1]
## [1,] 1
## [2,] 2
## [3,] 3
## [4,] 4
## [5,] 5
as.data.frame(x)
## x
## 1 1
## 2 2
## 3 3
## 4 4
## 5 5
Constants
Some constants already built in R:
letters[1:10] #small letters
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
LETTERS[1:40] #capital letters
## [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q"
## [18] "R" "S" "T" "U" "V" "W" "X" "Y" "Z" NA NA NA NA NA NA NA NA
## [35] NA NA NA NA NA NA
month.abb #months' abbrebiation
## [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov"
## [12] "Dec"
month.abb[4:10]
## [1] "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct"
month.abb[2]
## [1] "Feb"
month.name #months' name
## [1] "January" "February" "March" "April" "May"
## [6] "June" "July" "August" "September" "October"
## [11] "November" "December"
month.name[3]
## [1] "March"
pi #pi value
## [1] 3.141593
pi/2
## [1] 1.570796