• 1 Summary of previous sessions and Exercises
    • 1.1 Summary
    • 1.2 Solution to Exercise 1: Lets put "Ñ" between N and O
    • 1.3 New Exercise: Now lets repeat this after changing LETTERS into factor
  • 2 Vectors, matrices and arrays
    • 2.1 VECTORS
      • 2.1.1 THE "COMBNE" FUNCTION
      • 2.1.2 NUMERIC SEQUENCES
      • 2.1.3 REPEAT ELEMENTS IN A VECTOR
      • 2.1.4 RANDOM NUMBERS
      • 2.1.5 From specific distributions
      • 2.1.6 NAMING VECTORS
      • 2.1.7 OPERATIONS WITH VECTORS
      • 2.1.8 BASIC SUMMARY STATISTICS
      • 2.1.9 COMPARING VECTORS
      • 2.1.10 VECTOR POSITIONS
    • 2.2 MATRICES
      • 2.2.1 CREATING MATRICES
      • 2.2.2 NAMING MATRICES
      • 2.2.3 MATRIX POSITIONS
      • 2.2.4 OPERATIONS WITH MATRICES
      • 2.2.5 SUMMARY STATISTICS
    • 2.3 ARRAYS

Vectors Matrices and arrays

Back to main page

1 Summary of previous sessions and Exercises

1.1 Summary

v1<-c(T,T,F,F,F)
v2<-c(v1,T,T)
v3<-c(T,v1,T)

v1
## [1]  TRUE  TRUE FALSE FALSE FALSE
v2
## [1]  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE
v3
## [1]  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE

1.2 Solution to Exercise 1: Lets put "Ñ" between N and O

c(LETTERS,Ñ)
## Error in eval(expr, envir, enclos): object 'Ñ' not found
c(LETTERS,"Ñ")
##  [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" "Ñ"
length(LETTERS)
## [1] 26
c(LETTERS[1:14],"Ñ",LETTERS[16:length(LETTERS)])
##  [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "Ñ" "P" "Q" "R" "S"
## [20] "T" "U" "V" "W" "X" "Y" "Z"

1.3 New Exercise: Now lets repeat this after changing LETTERS into factor

factorLETTERS <- factor(LETTERS)

R is powerful to apply some task to objects with multiple elements:

LETTERS=="O" which(LETTERS=="O"==TRUE) which((LETTERS=="O")==TRUE) which((LETTERS=="O")) which(LETTERS=="O") which(!(LETTERS=="O")==FALSE) which(!(LETTERS!="O")!=FALSE)

Back to main page

2 Vectors, matrices and arrays

2.1 VECTORS

A vector is a simple set of elements (numbers, characters,..) such as the measurements of a single variable A vector is a sequence of data elements of the same basic type A vector is defined by its mode and length

2.1.1 THE "COMBNE" FUNCTION

use the function c() to place the elements separated by commas

a numeric vector

x <- c(20, 50, 100)  
x
## [1]  20  50 100

a logical vector

x <- c(TRUE, FALSE, TRUE, FALSE, FALSE)
x
## [1]  TRUE FALSE  TRUE FALSE FALSE

a vector of characters

x <- c("GG","TT","GA","AT","CC")
x
## [1] "GG" "TT" "GA" "AT" "CC"

BUT a vector cannot mix different types of elements

x <- c(2,3,5)
y <- c("a", "b", "c")
z <- c(x,y)
z
## [1] "2" "3" "5" "a" "b" "c"

numbers are coerced to characters to maintain the same type of elements (Remember as.XXX functions. R do that by default, but in a particular way)

class(x)
## [1] "numeric"
class(y)
## [1] "character"
class(z)
## [1] "character"

a vector can be defined by its mode() and its lenght() Both are shown by str()

mode(x)
## [1] "numeric"
length(x)
## [1] 3
str(x)
##  num [1:3] 2 3 5

2.1.2 NUMERIC SEQUENCES

The most simple way of doing sequences is using ":"

x <- 1:20
x
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

Note the difference between:

1:20-1  
##  [1]  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19
1:(20-1)
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19

Another way of getting a sequence is using the seq function (always check the help file the first time you use a function):

Use the seq() function (and see the manual)

?seq
seq(0, 10, by=0.1)
##   [1]  0.0  0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  1.0  1.1  1.2  1.3  1.4
##  [16]  1.5  1.6  1.7  1.8  1.9  2.0  2.1  2.2  2.3  2.4  2.5  2.6  2.7  2.8  2.9
##  [31]  3.0  3.1  3.2  3.3  3.4  3.5  3.6  3.7  3.8  3.9  4.0  4.1  4.2  4.3  4.4
##  [46]  4.5  4.6  4.7  4.8  4.9  5.0  5.1  5.2  5.3  5.4  5.5  5.6  5.7  5.8  5.9
##  [61]  6.0  6.1  6.2  6.3  6.4  6.5  6.6  6.7  6.8  6.9  7.0  7.1  7.2  7.3  7.4
##  [76]  7.5  7.6  7.7  7.8  7.9  8.0  8.1  8.2  8.3  8.4  8.5  8.6  8.7  8.8  8.9
##  [91]  9.0  9.1  9.2  9.3  9.4  9.5  9.6  9.7  9.8  9.9 10.0
seq(0, 10, 0.1)
##   [1]  0.0  0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  1.0  1.1  1.2  1.3  1.4
##  [16]  1.5  1.6  1.7  1.8  1.9  2.0  2.1  2.2  2.3  2.4  2.5  2.6  2.7  2.8  2.9
##  [31]  3.0  3.1  3.2  3.3  3.4  3.5  3.6  3.7  3.8  3.9  4.0  4.1  4.2  4.3  4.4
##  [46]  4.5  4.6  4.7  4.8  4.9  5.0  5.1  5.2  5.3  5.4  5.5  5.6  5.7  5.8  5.9
##  [61]  6.0  6.1  6.2  6.3  6.4  6.5  6.6  6.7  6.8  6.9  7.0  7.1  7.2  7.3  7.4
##  [76]  7.5  7.6  7.7  7.8  7.9  8.0  8.1  8.2  8.3  8.4  8.5  8.6  8.7  8.8  8.9
##  [91]  9.0  9.1  9.2  9.3  9.4  9.5  9.6  9.7  9.8  9.9 10.0
seq(length=9, from=1, to=5)
## [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
seq(from=1, to=5, length=9)
## [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
?sequence
sequence(5)
## [1] 1 2 3 4 5
sequence(3:5)
##  [1] 1 2 3 1 2 3 4 1 2 3 4 5
sequence(c(3,10))
##  [1]  1  2  3  1  2  3  4  5  6  7  8  9 10

2.1.3 REPEAT ELEMENTS IN A VECTOR

Use the rep() function (and see the manual)

?rep
rep(1, 50)
##  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [39] 1 1 1 1 1 1 1 1 1 1 1 1
rep(1:4, 2)
## [1] 1 2 3 4 1 2 3 4
rep(1:4, each=2) 
## [1] 1 1 2 2 3 3 4 4
rep(1:4, c(2,4, 5, 3))
##  [1] 1 1 2 2 2 2 3 3 3 3 3 4 4 4
rep(1:4, each=2, times=10)
##  [1] 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3
## [39] 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2
## [77] 3 3 4 4

Also works with characters, and using other vectors

y <- c("a", "b", "c")
r <- rep(z,3)  

2.1.4 RANDOM NUMBERS

?sample
x <- sample(1:500, 100, replace=F)
y <- sample(1:50, 100, replace=T)
duplicated(y)
##   [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
##  [13] FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE
##  [25] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE
##  [37] FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [49]  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE
##  [61] FALSE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE
##  [73]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
##  [85] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
##  [97]  TRUE  TRUE  TRUE  TRUE
sample(r, 20, replace = T)
##  [1] "2" "5" "c" "c" "b" "a" "c" "c" "b" "3" "b" "2" "2" "c" "a" "2" "a" "a" "3"
## [20] "a"

2.1.5 From specific distributions

?runif
x <- runif(10)
x
##  [1] 0.7547673509 0.4802415257 0.9675340578 0.3461869529 0.4061374068
##  [6] 0.5225130052 0.0006419218 0.4113089459 0.5704809022 0.1893039269
runif(100, min=0, max=500)
##   [1] 176.95977 486.34347 148.27615 493.91844  96.28272 264.97740 342.55956
##   [8] 463.68962 436.32608 296.16517 353.59719 431.74808 115.95900 114.93181
##  [15] 260.29344 372.65964 360.54442 222.38695 360.09339 340.83240 219.69729
##  [22] 148.79276 311.28448  22.06210 390.96326 243.57071  44.44462 112.90842
##  [29] 434.10187 439.90729 170.38263 472.24584 234.74566 432.15392 319.60377
##  [36] 100.63898 479.80532 183.78267 396.69186 287.91604 217.53279  82.14208
##  [43] 195.82659  73.78887 480.36894 335.63607 308.21186 441.28738 256.63126
##  [50]  70.98201 421.30677 180.96873 324.99954  96.79220  43.31118 430.60093
##  [57] 431.67907  14.35115 386.90865 141.41123 320.80879 275.54446 318.45716
##  [64] 217.13967 410.89555  88.56628 244.65470  26.03223  39.75040 184.36895
##  [71] 294.75850 198.20253  12.50980 313.10689 197.13736 277.25990 189.17771
##  [78] 212.94270 260.01184 219.32123 284.55371 342.66252 261.13149 218.58564
##  [85] 454.45605 482.23370 270.32259 162.61840 279.24024 454.07606 317.27018
##  [92] 432.31999 431.80939 216.68569 212.92658  83.94100 395.56211 412.95816
##  [99] 403.67488 191.65481
?rnorm
rnorm(10)
##  [1] -0.89523655 -0.72135193 -0.05590455  1.31798696 -0.27014890  0.07327648
##  [7]  0.05054044 -2.20449437 -0.85413235 -0.71628145
x <- rnorm(100, mean=25, sd=2)  
hist(x)

?rpois
?rexp
?Distributions

# The seed - important note on random sampling!
?set.seed
x<-rnorm(100)
x<-rnorm(100)

set.seed(123)
x<-rnorm(100)
set.seed(123)
x<-rnorm(100)

2.1.6 NAMING VECTORS

size1 <- c(20,50,100, 25,5,20)
size1
## [1]  20  50 100  25   5  20
country <- c("Portugal", "Spain", "France", "Italy", "Denmark", "Finland")

names(size1) <- country
size1
## Portugal    Spain   France    Italy  Denmark  Finland 
##       20       50      100       25        5       20

2.1.7 OPERATIONS WITH VECTORS

operation with vectors are made elemntwise

a <- c(10,30,50,70,25)
5+a
## [1] 15 35 55 75 30
5*a
## [1]  50 150 250 350 125
a/5
## [1]  2  6 10 14  5
b <- seq(5, 25, by=5)
c <- a+b
c
## [1] 15 40 65 90 50

RECYCLING : when two vectors are of unequal length, the shorter one will be recycled in order to match the longer vector

d <- c(20, 5)

a-d
## Warning in a - d: longer object length is not a multiple of shorter object
## length
## [1] -10  25  30  65   5
a*d
## Warning in a * d: longer object length is not a multiple of shorter object
## length
## [1]  200  150 1000  350  500

VERY IMPORTANT: ALWAYS READ WARNINGS AND ERRORS!!!

2.1.8 BASIC SUMMARY STATISTICS

size1 <- c(20,50,100, 25,5,20)
size2 <- c(12,15,10, 5,5,15)

sum(size1)
## [1] 220
mean(size2)
## [1] 10.33333
sd(size2)
## [1] 4.546061
var(size1)
## [1] 1176.667

2.1.9 COMPARING VECTORS

size1 == size2
## [1] FALSE FALSE FALSE FALSE  TRUE FALSE
size1 > size2
## [1]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
size2 <= size1
## [1] TRUE TRUE TRUE TRUE TRUE TRUE
size1%in%size2
## [1] FALSE FALSE FALSE FALSE  TRUE FALSE

ADD MORE ABOUT %in%

2.1.10 VECTOR POSITIONS

Retrive the values of an element of a vector indicating its position in brackets []. Elements can be indicated by a single number (note that negative values "exclude" elements), a set of numbers (use c() to create a vector for that), and also by name;

size1[1]
## [1] 20
size2[3]
## [1] 10
size1[10]
## [1] NA
size1[c(1,3)]
## [1]  20 100
size2[-1]      
## [1] 15 10  5  5 15
size2["Portugal"]
## [1] NA
size1["Portugal"]
## [1] NA
size2
## [1] 12 15 10  5  5 15
size2[3] <- 60
size2
## [1] 12 15 60  5  5 15

Back to main page

2.2 MATRICES

A matrix is a two-dimensional array (two dimmensions) of numbers or characters

A matrix is a collection of vectors (e.g. measurements of several variables for the same objects)

A matrix can only contain data of the same type

Let's define three vectors:

V1 containing numbers form 1 to 5

V2 containing numbers form 100 to 108 with a step of 2

V3 containing the number 50 five times

V1 <- c(1:5)
V2 <- seq(100,108,2)
V3 <- rep(50,5)

2.2.1 CREATING MATRICES

?rbind
rbind(V1,V2,V3)
##    [,1] [,2] [,3] [,4] [,5]
## V1    1    2    3    4    5
## V2  100  102  104  106  108
## V3   50   50   50   50   50
?cbind
cbind(V1,V2,V3)
##      V1  V2 V3
## [1,]  1 100 50
## [2,]  2 102 50
## [3,]  3 104 50
## [4,]  4 106 50
## [5,]  5 108 50

Recicling also works in this case

V4 <- 10:16
rbind(V1,V4)
## Warning in rbind(V1, V4): number of columns of result is not a multiple of
## vector length (arg 1)
##    [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## V1    1    2    3    4    5    1    2
## V4   10   11   12   13   14   15   16

Be careful, because the warning is shown only if they are fractionally recycled:

V5 <- 1:3
V6 <- 1:6
rbind(V5,V6)
##    [,1] [,2] [,3] [,4] [,5] [,6]
## V5    1    2    3    1    2    3
## V6    1    2    3    4    5    6
?matrix
matrix(nrow=3, ncol=5)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]   NA   NA   NA   NA   NA
## [2,]   NA   NA   NA   NA   NA
## [3,]   NA   NA   NA   NA   NA
matrix(0, nrow=3, ncol=5)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    0    0    0    0    0
## [2,]    0    0    0    0    0
## [3,]    0    0    0    0    0
matrix(1:15, nrow=3, ncol=5)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    4    7   10   13
## [2,]    2    5    8   11   14
## [3,]    3    6    9   12   15
matrix(c(1:15), nrow=3)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    4    7   10   13
## [2,]    2    5    8   11   14
## [3,]    3    6    9   12   15

But careful, to define a matrix, you need dimensions!

poo<-matrix(c(1:15))
poo
##       [,1]
##  [1,]    1
##  [2,]    2
##  [3,]    3
##  [4,]    4
##  [5,]    5
##  [6,]    6
##  [7,]    7
##  [8,]    8
##  [9,]    9
## [10,]   10
## [11,]   11
## [12,]   12
## [13,]   13
## [14,]   14
## [15,]   15
matrix(1:15, nrow=3, ncol=5, byrow=TRUE)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    2    3    4    5
## [2,]    6    7    8    9   10
## [3,]   11   12   13   14   15
matrix(c(0,1), nrow=3, ncol=5)
## Warning in matrix(c(0, 1), nrow = 3, ncol = 5): data length [2] is not a sub-
## multiple or multiple of the number of rows [3]
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    0    1    0    1    0
## [2,]    1    0    1    0    1
## [3,]    0    1    0    1    0
matrix(c(0,1), nrow=3, ncol=6)
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    0    1    0    1    0    1
## [2,]    1    0    1    0    1    0
## [3,]    0    1    0    1    0    1
matrix(1:17,nrow=3, ncol=5)
## Warning in matrix(1:17, nrow = 3, ncol = 5): data length [17] is not a sub-
## multiple or multiple of the number of rows [3]
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    4    7   10   13
## [2,]    2    5    8   11   14
## [3,]    3    6    9   12   15
mat <- matrix(1:17, nrow=3)
## Warning in matrix(1:17, nrow = 3): data length [17] is not a sub-multiple or
## multiple of the number of rows [3]
mat
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    4    7   10   13   16
## [2,]    2    5    8   11   14   17
## [3,]    3    6    9   12   15    1
# Spot the error here:
matrix(V1,V2,V3,nrow=3,ncol=5)
## Error in matrix(V1, V2, V3, nrow = 3, ncol = 5): 'dimnames' must be a list
matrix(c(V1,V2,V3),nrow=3,ncol=5)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    4  102  108   50
## [2,]    2    5  104   50   50
## [3,]    3  100  106   50   50
rbind(V1,V2,V3)
##    [,1] [,2] [,3] [,4] [,5]
## V1    1    2    3    4    5
## V2  100  102  104  106  108
## V3   50   50   50   50   50

2.2.2 NAMING MATRICES

mat1 <- rbind(V1,V2,V3)
mat2 <- cbind(V1,V2,V3)
mat3 <- matrix(c(V1,V2,V3),nrow=3,ncol=5,byrow=T)
mat4 <- matrix(c(V1,V2,V3),nrow=5,ncol=3)

mat1
##    [,1] [,2] [,3] [,4] [,5]
## V1    1    2    3    4    5
## V2  100  102  104  106  108
## V3   50   50   50   50   50
mat2
##      V1  V2 V3
## [1,]  1 100 50
## [2,]  2 102 50
## [3,]  3 104 50
## [4,]  4 106 50
## [5,]  5 108 50
mat3
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    2    3    4    5
## [2,]  100  102  104  106  108
## [3,]   50   50   50   50   50
mat4
##      [,1] [,2] [,3]
## [1,]    1  100   50
## [2,]    2  102   50
## [3,]    3  104   50
## [4,]    4  106   50
## [5,]    5  108   50
rownames(mat1)
## [1] "V1" "V2" "V3"
rownames(mat2)
## NULL
colnames(mat2)
## [1] "V1" "V2" "V3"
rownames(mat3)
## NULL
colnames(mat3)
## NULL
dimnames(mat1)
## [[1]]
## [1] "V1" "V2" "V3"
## 
## [[2]]
## NULL
dimnames(mat4)
## NULL
colnames(mat4) <- colnames(mat2)
rownames(mat4) <- country[-6]
dimnames(mat4)
## [[1]]
## [1] "Portugal" "Spain"    "France"   "Italy"    "Denmark" 
## 
## [[2]]
## [1] "V1" "V2" "V3"
mat4
##          V1  V2 V3
## Portugal  1 100 50
## Spain     2 102 50
## France    3 104 50
## Italy     4 106 50
## Denmark   5 108 50

EXERCISE: Create a matrix with 5 rows and 4 columns
containing data from an RNA expression experiment.
Create random data to fill it in
Rows are four treatments and columns are number of reads
for different genes.
my.exp <- matrix(sample(1:1000,20),nrow=5,ncol=4)
rownames(my.exp) <- paste("Treatment",1:5,sep="")
colnames(my.exp) <- paste("Gene",1:4,sep="")

2.2.3 MATRIX POSITIONS

dim(mat4)
## [1] 5 3
mat4[1]
## [1] 1
mat4[8]
## [1] 104
mat4[1, 1]
## [1] 1
mat4[1, 2]
## [1] 100
mat4[3, 2]
## [1] 104
mat4["France", "V2"]
## [1] 104

2.2.4 OPERATIONS WITH MATRICES

Also works elementwise

mat1
##    [,1] [,2] [,3] [,4] [,5]
## V1    1    2    3    4    5
## V2  100  102  104  106  108
## V3   50   50   50   50   50
mat1 + 4
##    [,1] [,2] [,3] [,4] [,5]
## V1    5    6    7    8    9
## V2  104  106  108  110  112
## V3   54   54   54   54   54
mat1 * 10
##    [,1] [,2] [,3] [,4] [,5]
## V1   10   20   30   40   50
## V2 1000 1020 1040 1060 1080
## V3  500  500  500  500  500
mat1 + mat3
##    [,1] [,2] [,3] [,4] [,5]
## V1    2    4    6    8   10
## V2  200  204  208  212  216
## V3  100  100  100  100  100
mat1 + mat2
## Error in mat1 + mat2: non-conformable arrays
mat1*mat3
##     [,1]  [,2]  [,3]  [,4]  [,5]
## V1     1     4     9    16    25
## V2 10000 10404 10816 11236 11664
## V3  2500  2500  2500  2500  2500
mat1%*%mat3
## Error in mat1 %*% mat3: non-conformable arguments
mat1*mat2
## Error in mat1 * mat2: non-conformable arrays
mat1%*%mat2 #  dimensions must be compatible for that
##      V1    V2    V3
## V1   55  1580   750
## V2 1580 54120 26000
## V3  750 26000 12500

2.2.5 SUMMARY STATISTICS

mean(mat1)
## [1] 52.33333
sd(mat1)
## [1] 42.75623
median(mat1)
## [1] 50

The previous lines provide values for the entire matrix. To get values per rows and columns:

colSums(mat1)
## [1] 151 154 157 160 163
rowSums(mat1)
##  V1  V2  V3 
##  15 520 250
colMeans(mat1)
## [1] 50.33333 51.33333 52.33333 53.33333 54.33333
rowMeans(mat1)
##  V1  V2  V3 
##   3 104  50

These functions are the same that using the apply() function:

?apply

There is a set of apply-related functions, all with the same rationale: applying a function to an multiple-element object. For example:

apply(mat1,1,mean)
##  V1  V2  V3 
##   3 104  50
apply(mat1,2,mean)
## [1] 50.33333 51.33333 52.33333 53.33333 54.33333

And you can estimate other statistics or functions (even your own customized functions) using apply

apply(mat1, 2, sd)
## [1] 49.50084 50.01333 50.54041 51.08163 51.63655
apply(mat1, 2, median)
## [1] 50 50 50 50 50

Back to main page

2.3 ARRAYS

An array is a three-dimensional object (three dimmension) of numbers or characters

An array is a collection of matrices (e.g. measurements of several variables for the same objects, across treatments)

An array can only contain data of the same type

?array
ARR <- array(1:12, dim=c(2,3,2))
dim(V1)
## NULL
dim(mat1)
## [1] 3 5
dim(ARR)
## [1] 2 3 2
ARR
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]    7    9   11
## [2,]    8   10   12
EXERCISE: Create an array containing 3 matrices
with 5 rows and 4 columns each containing data
from an RNA expression experiment at different times. 
Rows are four treatments and columns are number of reads
for different genes, and the third dimension is time.
my.exp1 <- my.exp
my.exp2 <- matrix(sample(1:1000,20), nrow=5, ncol=4)
my.exp3 <- matrix(sample(1:1000,20), nrow=5, ncol=4)

my.array <- array(c(my.exp1,my.exp2,my.exp3), dim=c(5,4,3))
dimnames(my.array) <- list(paste("Treatment",1:5), paste("Gene",1:4), paste("Time",0:2))

my.array
## , , Time 0
## 
##             Gene 1 Gene 2 Gene 3 Gene 4
## Treatment 1    286    344    872    876
## Treatment 2    606    310    195    534
## Treatment 3    500    459    861    177
## Treatment 4    985    944    164    554
## Treatment 5    784     20     52    827
## 
## , , Time 1
## 
##             Gene 1 Gene 2 Gene 3 Gene 4
## Treatment 1     84    302    589    428
## Treatment 2    523    597    430    672
## Treatment 3    633    877    710    250
## Treatment 4    951    706    761    804
## Treatment 5    392    619    712    429
## 
## , , Time 2
## 
##             Gene 1 Gene 2 Gene 3 Gene 4
## Treatment 1    398     40    978    186
## Treatment 2    528    936    125    573
## Treatment 3    983    522    265    252
## Treatment 4    381    473    775    458
## Treatment 5    545    200    903    152
str(my.array)
##  int [1:5, 1:4, 1:3] 286 606 500 985 784 344 310 459 944 20 ...
##  - attr(*, "dimnames")=List of 3
##   ..$ : chr [1:5] "Treatment 1" "Treatment 2" "Treatment 3" "Treatment 4" ...
##   ..$ : chr [1:4] "Gene 1" "Gene 2" "Gene 3" "Gene 4"
##   ..$ : chr [1:3] "Time 0" "Time 1" "Time 2"
apply(my.array, 2, mean)
##   Gene 1   Gene 2   Gene 3   Gene 4 
## 571.9333 489.9333 559.4667 478.1333
apply(my.array, 3, mean)
## Time 0 Time 1 Time 2 
## 517.50 573.45 483.65
apply(my.array, 1, mean)
## Treatment 1 Treatment 2 Treatment 3 Treatment 4 Treatment 5 
##    448.5833    502.4167    540.7500    663.0000    469.5833

Back to main page