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

What's hot

6. R data structures
6. R data structures6. R data structures
6. R data structures
ExternalEvents
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng웅식 전
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
sandhya yadav
 
Arrays in c
Arrays in cArrays in c
Arrays in c
Jeeva Nanthini
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
Rajendran
 
Manipulating Data using base R package
Manipulating Data using base R package Manipulating Data using base R package
Manipulating Data using base R package
Rupak Roy
 
Files,blocks and functions in R
Files,blocks and functions in RFiles,blocks and functions in R
Files,blocks and functions in R
Vladimir Bakhrushin
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
Neeru Mittal
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
Gagan Deep
 
Arrays
ArraysArrays
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
Maulen Bale
 
Transpose and manipulate character strings
Transpose and manipulate character strings Transpose and manipulate character strings
Transpose and manipulate character strings
Rupak Roy
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
Rajendran
 
Basic array in c programming
Basic array in c programmingBasic array in c programming
Basic array in c programming
Sajid Hasan
 
Handling Missing Values
Handling Missing Values Handling Missing Values
Handling Missing Values
Rupak Roy
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
Avjinder (Avi) Kaler
 
3 Data Structure in R
3 Data Structure in R3 Data Structure in R
3 Data Structure in R
Dr Nisha Arora
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
eShikshak
 
Array in c
Array in cArray in c
Array in c
Ravi Gelani
 

What's hot (20)

6. R data structures
6. R data structures6. R data structures
6. R data structures
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Manipulating Data using base R package
Manipulating Data using base R package Manipulating Data using base R package
Manipulating Data using base R package
 
Files,blocks and functions in R
Files,blocks and functions in RFiles,blocks and functions in R
Files,blocks and functions in R
 
R learning by examples
R learning by examplesR learning by examples
R learning by examples
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
Arrays
ArraysArrays
Arrays
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 
Transpose and manipulate character strings
Transpose and manipulate character strings Transpose and manipulate character strings
Transpose and manipulate character strings
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
 
Basic array in c programming
Basic array in c programmingBasic array in c programming
Basic array in c programming
 
Handling Missing Values
Handling Missing Values Handling Missing Values
Handling Missing Values
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
 
3 Data Structure in R
3 Data Structure in R3 Data Structure in R
3 Data Structure in R
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
 
Array in c
Array in cArray in c
Array in c
 

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

R programming
R programmingR programming
R programming
Pramodkumar Jha
 
A quick introduction to R
A quick introduction to RA quick introduction to R
A quick introduction to R
Angshuman Saha
 
R Programming.pptx
R Programming.pptxR Programming.pptx
R Programming.pptx
kalai75
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
Chu An
 
Chapter 3 Built-in Data Structures, Functions, and Files .pptx
Chapter 3 Built-in Data Structures, Functions, and Files .pptxChapter 3 Built-in Data Structures, Functions, and Files .pptx
Chapter 3 Built-in Data Structures, Functions, and Files .pptx
SovannDoeur
 
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavSeminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavVyacheslav Arbuzov
 
Programming in R
Programming in RProgramming in R
Programming in R
Smruti Sarangi
 
R programming language
R programming languageR programming language
R programming language
Alberto Minetti
 
R training3
R training3R training3
R training3
Hellen Gakuruh
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
Josh Doyle
 
data frames.pptx
data frames.pptxdata frames.pptx
data frames.pptx
RacksaviR
 
R Cheat Sheet – Data Management
R Cheat Sheet – Data ManagementR Cheat Sheet – Data Management
R Cheat Sheet – Data Management
Dr. Volkan OBAN
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environment
Yogendra Chaubey
 
R gráfico
R gráficoR gráfico
R gráfico
stryper1968
 
Data manipulation on r
Data manipulation on rData manipulation on r
Data manipulation on r
Abhik Seal
 

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

R programming
R programmingR programming
R programming
 
A quick introduction to R
A quick introduction to RA quick introduction to R
A quick introduction to R
 
R Programming.pptx
R Programming.pptxR Programming.pptx
R Programming.pptx
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
Chapter 3 Built-in Data Structures, Functions, and Files .pptx
Chapter 3 Built-in Data Structures, Functions, and Files .pptxChapter 3 Built-in Data Structures, Functions, and Files .pptx
Chapter 3 Built-in Data Structures, Functions, and Files .pptx
 
R교육1
R교육1R교육1
R교육1
 
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavSeminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
 
Programming in R
Programming in RProgramming in R
Programming in R
 
proj1v2
proj1v2proj1v2
proj1v2
 
R programming language
R programming languageR programming language
R programming language
 
R
RR
R
 
R training3
R training3R training3
R training3
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
 
data frames.pptx
data frames.pptxdata frames.pptx
data frames.pptx
 
R Cheat Sheet – Data Management
R Cheat Sheet – Data ManagementR Cheat Sheet – Data Management
R Cheat Sheet – Data Management
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environment
 
20100528
2010052820100528
20100528
 
20100528
2010052820100528
20100528
 
R gráfico
R gráficoR gráfico
R gráfico
 
Data manipulation on r
Data manipulation on rData manipulation on r
Data manipulation on r
 

More from Adrien Melquiond

Day 1a welcome introduction
Day 1a   welcome   introductionDay 1a   welcome   introduction
Day 1a welcome introduction
Adrien Melquiond
 
R course ggplot2
R course   ggplot2R course   ggplot2
R course ggplot2
Adrien Melquiond
 
Day 5a iteration and functions if().pptx
Day 5a   iteration and functions  if().pptxDay 5a   iteration and functions  if().pptx
Day 5a iteration and functions if().pptx
Adrien Melquiond
 
Day 4b iteration and functions for-loops.pptx
Day 4b   iteration and functions  for-loops.pptxDay 4b   iteration and functions  for-loops.pptx
Day 4b iteration and functions for-loops.pptx
Adrien Melquiond
 
Day 4a iteration and functions.pptx
Day 4a   iteration and functions.pptxDay 4a   iteration and functions.pptx
Day 4a iteration and functions.pptx
Adrien Melquiond
 
Day 3 plotting.pptx
Day 3   plotting.pptxDay 3   plotting.pptx
Day 3 plotting.pptx
Adrien Melquiond
 

More from Adrien Melquiond (6)

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

Recently uploaded

Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 

Recently uploaded (20)

Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 

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