SlideShare a Scribd company logo
R structures & objects: matrices and
data frames
Day 1 - Introduction to R for Life Sciences
Matrices
A matrix is a “vector in the shape of a table”
All items in the matrix are the same data type
Can be built from rows using rbind(), or from columns using cbind(),
or using matrix()
> rbind( 1:3, 11:13)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 11 12 13
> cbind(11:13, 23:25)
[,1] [,2]
[1,] 11 23
[2,] 12 24
[3,] 13 25
Using the matrix function
> x <- matrix(1:6, nrow=2, byrow=TRUE)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
Row and column names make life easier!
> x <- matrix(1:6, nrow=2, byrow=TRUE,
dimnames=list( c(“geneA”, “geneB”), c(“delA”, “delB”, “delC”))
delA delB delC
geneA 1 2 3
geneB 4 5 6
Data structures - data.frames
Data.frame: a more general form of a matrix, its columns can be
different type
> id <- c(1, 2, 3, 4)
> color <- c("red", "green", "blue", NA)
> passed <- c(TRUE, TRUE, TRUE, FALSE)
> mydata <- data.frame(id, color, passed)
id color passed
1 1 red TRUE
2 2 green TRUE
3 3 blue TRUE
4 4 <NA> FALSE
Operations are always element-wise
> a <- 1:3
> b <- 4:6
> a + b
5 7 9
> b^a # ‘raised to power’
4 25 216
> p <- matrix(1:4, ncol=2,
byrow=TRUE)
> q <- cbind(c(10, 10), c(100,100))
> p*q
[,1] [,2]
[1,] 10 200
[2,] 30 400
Useful functions
str() # display the data structure
summary() # display a summary of the data
length() # get the length of a vector or list (data.frame: nrow!)
dim() # get the dimensions of a data.frame or matrix
head() # show the first part of a data structure
You can also explore your data in the Environment window!
Dimensions of data.frames and matrices
> x <- matrix(1:6, nrow=2, byrow=TRUE)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
x[ i, j ]
index before the comma: indicates the row(s). If missing: all rows
index after the comma: indicates column(s). If missing: all columns
Example
> x
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
Using integers:
> x[2, 3] # the value on the second row, third column
> x[ , 2] # all rows, second column. So: the whole 2nd column
> x[ , c(1,3)] # the first and third column (new data.frame or matrix!)
> x[ , -2] # everything but the second column
> x[ , 1:3] # first up to and including third column
Data Frames
data.frames
> mydata[ , "id"]
> mydata$id # does the same thing
Using logicals:
delA delB delC
geneA 1 2 3
geneB 4 5 6
> ind <- c(FALSE, TRUE, TRUE)
> x[ 1 , ind] # first row; first column:no, 2nd, 3rd column: yes
[1] 2 3
Using characters:
> x <- matrix(1:6, nrow=2, byrow=TRUE,
dimnames=list( c("geneA", "geneB"), c("delA", "delB", "delC"))
delA delB delC
geneA 1 2 3
geneB 4 5 6
> x["geneB", "delA"] # selects the value of geneB in delA
> x[, c("delA", "delC")] # selects columns delA and delC
Logical vector and selection
Often (implicitly) used in combination with select statements
delA delB delC
geneA 1 2 3
geneB 4 5 6
> ind <- x["geneA", ] > 1
[1] FALSE TRUE TRUE
> x["geneA", ind]
[1] 2 3
Operators
delA delB delC
geneA 1 2 3
geneB 4 5 6
> ind <- x["geneA", ] > 1 & x["geneA", ] < 3
> x["geneA", ind]
[1] 2
Data structures - lists
An ordered collection of "things"
> a <- c(1, 2, 3, 4)
> mylist <- list(name="Patrick", numbers=a, age=38)
$name
[1] "Patrick"
$numbers
[1] 1 2 3 4
$age
[1] 38
Specifics for lists
lists
>mylist <- list(analysis=”GSEA”, genes=c(“Foxo3a”, “TP53”), cutoff=0.05)
> mylist$analysis
> mylist$genes[2]
Data types - factors
Factors deal with categorical variables
> gender <- factor(c(rep("male", 2), rep("female", 3)))
> gender
[1] male male female female female the actual values
Levels: female male allowed values
> str(gender)
Factor w/ 2 levels "female","male": 2 2 1 1 1
Ordering
(Re)order a data.frame or matrix using the values from a single
column using order()
> mydata <- data.frame( id=c(1,3,4,2), name=c("geneB", "geneA", "geneD",
"geneC"), value=c(-0.2, 1.5, -3, 3))
> mydata[order(mydata[, "id"]), ] # sort on id
> mydata[order(mydata[, "name"]), ] # sort on name

More Related Content

PDF
Day 1b R structures objects.pptx
PDF
Day 2 repeats.pptx
PDF
Day 1c access, select ordering copy.pptx
PDF
Day 2b i/o.pptx
PDF
Day 5b statistical functions.pptx
PPTX
Array,MULTI ARRAY, IN C
PDF
Data Types and Structures in R
PPT
Multi dimensional arrays
Day 1b R structures objects.pptx
Day 2 repeats.pptx
Day 1c access, select ordering copy.pptx
Day 2b i/o.pptx
Day 5b statistical functions.pptx
Array,MULTI ARRAY, IN C
Data Types and Structures in R
Multi dimensional arrays

What's hot (20)

PDF
6. R data structures
PDF
11 1. multi-dimensional array eng
PPTX
Introduction to Array ppt
PPTX
Arrays in c
PPT
Two dimensional array
PDF
Manipulating Data using base R package
PPTX
Files,blocks and functions in R
PDF
R learning by examples
PPTX
Two dimensional arrays
PPSX
C Programming : Arrays
PPTX
PPT
Data Structure Midterm Lesson Arrays
PDF
Transpose and manipulate character strings
PPT
One dimensional 2
PPTX
Basic array in c programming
PDF
Handling Missing Values
PDF
R code for data manipulation
PDF
3 Data Structure in R
PDF
Lecture17 arrays.ppt
PPT
Array in c
6. R data structures
11 1. multi-dimensional array eng
Introduction to Array ppt
Arrays in c
Two dimensional array
Manipulating Data using base R package
Files,blocks and functions in R
R learning by examples
Two dimensional arrays
C Programming : Arrays
Data Structure Midterm Lesson Arrays
Transpose and manipulate character strings
One dimensional 2
Basic array in c programming
Handling Missing Values
R code for data manipulation
3 Data Structure in R
Lecture17 arrays.ppt
Array in c
Ad

Similar to Day 1d R structures & objects: matrices and data frames.pptx (20)

PDF
R language, an introduction
PPTX
Language R
PDF
R training3
PPTX
R programming
PPTX
R교육1
PDF
3 R Tutorial Data Structure
PPTX
A quick introduction to R
PPTX
R programming Fundamentals
PPTX
R data-structures-3
PPTX
Introduction to R programming Language.pptx
PDF
R_CheatSheet.pdf
PPTX
DataStructures.pptx
PPTX
Introduction to R
PPTX
Big Data Mining in Indian Economic Survey 2017
PDF
20130215 Reading data into R
PPTX
Get started with R lang
PPTX
Data Types of R.pptx
PPT
PPT
Basics.pptNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
PPTX
Introduction to R _IMPORTANT FOR DATA ANALYTICS
R language, an introduction
Language R
R training3
R programming
R교육1
3 R Tutorial Data Structure
A quick introduction to R
R programming Fundamentals
R data-structures-3
Introduction to R programming Language.pptx
R_CheatSheet.pdf
DataStructures.pptx
Introduction to R
Big Data Mining in Indian Economic Survey 2017
20130215 Reading data into R
Get started with R lang
Data Types of R.pptx
Basics.pptNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
Introduction to R _IMPORTANT FOR DATA ANALYTICS
Ad

More from Adrien Melquiond (6)

PPTX
Day 1a welcome introduction
PDF
R course ggplot2
PDF
Day 5a iteration and functions if().pptx
PDF
Day 4b iteration and functions for-loops.pptx
PDF
Day 4a iteration and functions.pptx
PDF
Day 3 plotting.pptx
Day 1a welcome introduction
R course ggplot2
Day 5a iteration and functions if().pptx
Day 4b iteration and functions for-loops.pptx
Day 4a iteration and functions.pptx
Day 3 plotting.pptx

Recently uploaded (20)

PPTX
Lesson notes of climatology university.
PDF
Classroom Observation Tools for Teachers
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Cell Types and Its function , kingdom of life
PDF
01-Introduction-to-Information-Management.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Pre independence Education in Inndia.pdf
Lesson notes of climatology university.
Classroom Observation Tools for Teachers
STATICS OF THE RIGID BODIES Hibbelers.pdf
GDM (1) (1).pptx small presentation for students
O7-L3 Supply Chain Operations - ICLT Program
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Final Presentation General Medicine 03-08-2024.pptx
Microbial disease of the cardiovascular and lymphatic systems
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Complications of Minimal Access Surgery at WLH
Cell Types and Its function , kingdom of life
01-Introduction-to-Information-Management.pdf
PPH.pptx obstetrics and gynecology in nursing
FourierSeries-QuestionsWithAnswers(Part-A).pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Microbial diseases, their pathogenesis and prophylaxis
Pre independence Education in Inndia.pdf

Day 1d R structures & objects: matrices and data frames.pptx

  • 1. R structures & objects: matrices and data frames Day 1 - Introduction to R for Life Sciences
  • 2. Matrices A matrix is a “vector in the shape of a table” All items in the matrix are the same data type Can be built from rows using rbind(), or from columns using cbind(), or using matrix() > rbind( 1:3, 11:13) [,1] [,2] [,3] [1,] 1 2 3 [2,] 11 12 13 > cbind(11:13, 23:25) [,1] [,2] [1,] 11 23 [2,] 12 24 [3,] 13 25
  • 3. Using the matrix function > x <- matrix(1:6, nrow=2, byrow=TRUE) [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 Row and column names make life easier! > x <- matrix(1:6, nrow=2, byrow=TRUE, dimnames=list( c(“geneA”, “geneB”), c(“delA”, “delB”, “delC”)) delA delB delC geneA 1 2 3 geneB 4 5 6
  • 4. Data structures - data.frames Data.frame: a more general form of a matrix, its columns can be different type > id <- c(1, 2, 3, 4) > color <- c("red", "green", "blue", NA) > passed <- c(TRUE, TRUE, TRUE, FALSE) > mydata <- data.frame(id, color, passed) id color passed 1 1 red TRUE 2 2 green TRUE 3 3 blue TRUE 4 4 <NA> FALSE
  • 5. Operations are always element-wise > a <- 1:3 > b <- 4:6 > a + b 5 7 9 > b^a # ‘raised to power’ 4 25 216 > p <- matrix(1:4, ncol=2, byrow=TRUE) > q <- cbind(c(10, 10), c(100,100)) > p*q [,1] [,2] [1,] 10 200 [2,] 30 400
  • 6. Useful functions str() # display the data structure summary() # display a summary of the data length() # get the length of a vector or list (data.frame: nrow!) dim() # get the dimensions of a data.frame or matrix head() # show the first part of a data structure You can also explore your data in the Environment window!
  • 7. Dimensions of data.frames and matrices > x <- matrix(1:6, nrow=2, byrow=TRUE) [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 x[ i, j ] index before the comma: indicates the row(s). If missing: all rows index after the comma: indicates column(s). If missing: all columns
  • 8. Example > x [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 Using integers: > x[2, 3] # the value on the second row, third column > x[ , 2] # all rows, second column. So: the whole 2nd column > x[ , c(1,3)] # the first and third column (new data.frame or matrix!) > x[ , -2] # everything but the second column > x[ , 1:3] # first up to and including third column
  • 9. Data Frames data.frames > mydata[ , "id"] > mydata$id # does the same thing
  • 10. Using logicals: delA delB delC geneA 1 2 3 geneB 4 5 6 > ind <- c(FALSE, TRUE, TRUE) > x[ 1 , ind] # first row; first column:no, 2nd, 3rd column: yes [1] 2 3
  • 11. Using characters: > x <- matrix(1:6, nrow=2, byrow=TRUE, dimnames=list( c("geneA", "geneB"), c("delA", "delB", "delC")) delA delB delC geneA 1 2 3 geneB 4 5 6 > x["geneB", "delA"] # selects the value of geneB in delA > x[, c("delA", "delC")] # selects columns delA and delC
  • 12. Logical vector and selection Often (implicitly) used in combination with select statements delA delB delC geneA 1 2 3 geneB 4 5 6 > ind <- x["geneA", ] > 1 [1] FALSE TRUE TRUE > x["geneA", ind] [1] 2 3
  • 13. Operators delA delB delC geneA 1 2 3 geneB 4 5 6 > ind <- x["geneA", ] > 1 & x["geneA", ] < 3 > x["geneA", ind] [1] 2
  • 14. Data structures - lists An ordered collection of "things" > a <- c(1, 2, 3, 4) > mylist <- list(name="Patrick", numbers=a, age=38) $name [1] "Patrick" $numbers [1] 1 2 3 4 $age [1] 38
  • 15. Specifics for lists lists >mylist <- list(analysis=”GSEA”, genes=c(“Foxo3a”, “TP53”), cutoff=0.05) > mylist$analysis > mylist$genes[2]
  • 16. Data types - factors Factors deal with categorical variables > gender <- factor(c(rep("male", 2), rep("female", 3))) > gender [1] male male female female female the actual values Levels: female male allowed values > str(gender) Factor w/ 2 levels "female","male": 2 2 1 1 1
  • 17. Ordering (Re)order a data.frame or matrix using the values from a single column using order() > mydata <- data.frame( id=c(1,3,4,2), name=c("geneB", "geneA", "geneD", "geneC"), value=c(-0.2, 1.5, -3, 3)) > mydata[order(mydata[, "id"]), ] # sort on id > mydata[order(mydata[, "name"]), ] # sort on name