Ways to create a vector in R
seq(-1,1)
[1] -1 0 1
seq(-1,1,by=0.5)
[1] -1.0 -0.5 0.0 0.5 1.0
seq(-1,1,length=15)
[1] -1.0000000 -0.8571429 -0.7142857 -0.5714286 -0.4285714 -0.2857143 -0.1428571
[8] 0.0000000 0.1428571 0.2857143 0.4285714 0.5714286 0.7142857 0.8571429
[15] 1.0000000
Repetition of the same value n times
rep(2.1,3)
[1] 2.1 2.1 2.1
More complex ways
x<-c(1,11,3,5,8,12)
class(x)
[1] "numeric"
is.vector(x)
[1] TRUE
x1<-c(rep(0.1,3),4,seq(-1,1,by=0.5), 4:2)
x1
[1] 0.1 0.1 0.1 4.0 -1.0 -0.5 0.0 0.5 1.0 4.0 3.0 2.0
length(x1)
[1] 12
x<-0:5
y<-1:6
x-1
[1] -1 0 1 2 3 4
x+y
[1] 1 3 5 7 9 11
(x+y)^2
[1] 1 9 25 49 81 121
x<3
[1] TRUE TRUE TRUE FALSE FALSE FALSE
x<-0:5
y<-1:5
x+y
longer object length is not a multiple of shorter object length
[1] 1 3 5 7 9 6
index<-1:6
name<-paste(c("File","File2","File3","File4","File5","File6"))
paste(name,"txt",sep=".")
[1] "File.txt" "File2.txt" "File3.txt" "File4.txt" "File5.txt" "File6.txt"
class(x)
[1] "integer"
pop_prov<-c(1463249,1842963,2521681,574906,792619)
pop_prov
[1] 1463249 1842963 2521681 574906 792619
prov_Spain<-c("Murcia","Alicante","Valencia","Castellon","Tarragona")
names(pop_prov)
NULL
names(pop_prov)<-prov_Spain
names(pop_prov)
[1] "Murcia" "Alicante" "Valencia" "Castellon" "Tarragona"
pop_prov[c("Valencia")]
Valencia
2521681
Look for names in help to see how it works
?help
Matrices are a little more complex but you can create them easily matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
?matrix
matrix(1:9,nrow=3)
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
matrix(1:9,nrow=3,byrow = TRUE)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
matrix(1:10,nrow=3,ncol=4)
data length [10] is not a sub-multiple or multiple of the number of rows [3]
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 1
[3,] 3 6 9 2
The first numbers are reused to fill up the complete matrix
dd<-matrix(c(6.47,4.03,6.13,3.76,6.19))
cbind rbind
x<-0:5
x
[1] 0 1 2 3 4 5
y<-1:6
y
[1] 1 2 3 4 5 6
xx<-cbind(x,y)
xx
x y
[1,] 0 1
[2,] 1 2
[3,] 2 3
[4,] 3 4
[5,] 4 5
[6,] 5 6
xxx<-rbind(x,y)
xxx
[,1] [,2] [,3] [,4] [,5] [,6]
x 0 1 2 3 4 5
y 1 2 3 4 5 6
x1<-rbind(c(0,2,4),c(1,3,5))
x1
[,1] [,2] [,3]
[1,] 0 2 4
[2,] 1 3 5
x2<-matrix(seq(0,10,2),nrow=3)
x2
[,1] [,2]
[1,] 0 6
[2,] 2 8
[3,] 4 10
dim(x1)
[1] 2 3
dim(x2)
[1] 3 2
x1%*%x2
[,1] [,2]
[1,] 20 56
[2,] 26 80
t() diag()
t(x1)
[,1] [,2]
[1,] 0 1
[2,] 2 3
[3,] 4 5
Ask for a particular element
xxx
[,1] [,2] [,3] [,4] [,5] [,6]
x 0 1 2 3 4 5
y 1 2 3 4 5 6
xx[4,1]
x
3
xx[4,]
x y
3 4
xx[,2]
[1] 1 2 3 4 5 6
xx[xx>4]
[1] 5 5 6
x
[1] 0 1 2 3 4 5
x[-1]
[1] 1 2 3 4 5
x
[1] 0 1 2 3 4 5
xx[-1,]
x y
[1,] 1 2
[2,] 2 3
[3,] 3 4
[4,] 4 5
[5,] 5 6
xx[-1]
[1] 1 2 3 4 5 1 2 3 4 5 6
xx[-8]
[1] 0 1 2 3 4 5 1 3 4 5 6
length() get or set the length of vectors/matrices max() returns the maximum of vectors/matrices min() returns the minimum of vectors/matrices prod() returns the product of all the values present in its arguments cumsum() returns a vector whose elements are the cumulative sums cumprod() returns a vector whose elements are the cumulative products
length(xx)
[1] 12
max(xx)
[1] 6
min(xx)
[1] 0
prod(xx)
[1] 0
cumsum(xx)
[1] 0 1 3 6 10 15 16 18 21 25 30 36
cumprod(xx)
[1] 0 0 0 0 0 0 0 0 0 0 0 0
tolower("HOLA")
[1] "hola"
Now go to array, is a matrix with more levels or the other way around a matrix is an special case of matrix with just one level.
z<-array(1:6,c(2,3))
z
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
z<-matrix(1:6,nrow=2)
z
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
z<-array(1:24,c(2,3,4))
z
, , 1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
, , 2
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
, , 3
[,1] [,2] [,3]
[1,] 13 15 17
[2,] 14 16 18
, , 4
[,1] [,2] [,3]
[1,] 19 21 23
[2,] 20 22 24
array(data = NA, dim = length(data), dimnames = NULL)
z<-array(1:24,c(2,3,4),dimnames=list(c("a", "b"), c("A", "B", "C"),c("1","2","3","4")))
z
, , 1
A B C
a 1 3 5
b 2 4 6
, , 2
A B C
a 7 9 11
b 8 10 12
, , 3
A B C
a 13 15 17
b 14 16 18
, , 4
A B C
a 19 21 23
b 20 22 24
z<-array(1:24,c(2,3,4))
z
, , 1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
, , 2
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
, , 3
[,1] [,2] [,3]
[1,] 13 15 17
[2,] 14 16 18
, , 4
[,1] [,2] [,3]
[1,] 19 21 23
[2,] 20 22 24
z[1,,]
z[,1,]
z[,,1]
prov<-c(14:18)
prov
[1] 14 15 16 17 18
mode(prov)
[1] "numeric"
class(prov)
[1] "integer"
regioni<-factor(prov)
mode(prov)
[1] "numeric"
class(prov)
[1] "integer"
prov
[1] 14 15 16 17 18
levels(prov)
NULL
levels(prov)<-c("Murcia","Alicante","Valencia","Castellon","Tarragona")
prov
[1] 14 15 16 17 18
attr(,"levels")
[1] "Murcia" "Alicante" "Valencia" "Castellon" "Tarragona"
data.frames you can combine different kind of data
stringsAsFactors = FALSE
employee<-c("Joe Doe","Peter Gynn", "Jolie Hope")
str(employee)
chr [1:3] "Joe Doe" "Peter Gynn" "Jolie Hope"
salary<-c(21000,23400,26800)
startdate<-as.Date(c('2010-11-1','2008-3-25','2007-3-14'))
employ.data<-data.frame(employee,salary,startdate)
str(employ.data)
'data.frame': 3 obs. of 3 variables:
$ employee : Factor w/ 3 levels "Joe Doe","Jolie Hope",..: 1 3 2
$ salary : num 21000 23400 26800
$ startdate: Date, format: "2010-11-01" "2008-03-25" ...
data("airquality")
class("airquality")
[1] "character"
attributes("airquality")
NULL
dimnames("airquality")
NULL
summay
employ.data2<-list(employee,salary,startdate)
str(employ.data2)
List of 3
$ : chr [1:3] "Joe Doe" "Peter Gynn" "Jolie Hope"
$ : num [1:3] 21000 23400 26800
$ : Date[1:3], format: "2010-11-01" "2008-03-25" ...
employ.data2<-list(employees=employee,salary=salary,startdate=startdate)
str(employ.data2)
List of 3
$ employees: chr [1:3] "Joe Doe" "Peter Gynn" "Jolie Hope"
$ salary : num [1:3] 21000 23400 26800
$ startdate: Date[1:3], format: "2010-11-01" "2008-03-25" ...
employee
[1] "Joe Doe" "Peter Gynn" "Jolie Hope"
To extract elements from a list, in the case of named list,
employ.data2$employees
[1] "Joe Doe" "Peter Gynn" "Jolie Hope"
employ.data2[[1]]
[1] "Joe Doe" "Peter Gynn" "Jolie Hope"
employ.data2[1]
$employees
[1] "Joe Doe" "Peter Gynn" "Jolie Hope"
employ.data2[1:2]
$employees
[1] "Joe Doe" "Peter Gynn" "Jolie Hope"
$salary
[1] 21000 23400 26800