SlideShare a Scribd company logo
Access, select & ordering
Day 1 - Introduction to R for Life Sciences
Accessing vectors, matrices, data.frames
Positions within vectors, matrices and data.frames are accessed
using [ ]:
> v <- c(10, 3, 5, 10)
> v[2]
3
[] can also be used to assign (write) new values, e.g: v[2] <- 10
( ) are used for function calls (or grouping operators, more later) !!!
for instance: myvector <- c( ), mymatrix <- matrix( ), mydata <- data.frame( )
Three ways to access values from vectors, matrices
and data.frames
Integers: specify the positions of the elements you mean
Logical: specify (using TRUE/FALSE) which elements you want
Character: specify their names
only if your vector/matrix/data.frame has (unique) names!
All these selections are made with vectors.
They are sometimes called indexes.
Examples:
chromlength <- c(230218, 813184, 316620, 1531933)
Integer:
chromlength[ c(4, 2) ] => 1531933, 813184
Logical:
chromlength[ c(FALSE, FALSE, TRUE, FALSE) ] => 316620
Character:
names(chromlength) <- c("chrI", "chrII", "chrIII", "chrIV")
chromlength[ c("chrIII", "chrI") ] => 316620, 230218
Specifics for lists & data.frames
lists
>mylist <- list(analysis=”GSEA”, genes=c(“Foxo3a”, “TP53”), cutoff=0.05)
> mylist$analysis
> mylist$genes[2]
data.frames
> mydata[ , "id"]
> mydata$id # does the same thing
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
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
> x["geneA", x["geneA", ] > 1] # same as above, but implicit
Operators
< # Less than
> # Greater than
== # Equal to. Note: don’t confuse with = (assignment)
>= # Greater than or equal to
<= # Less than or equal to
& # AND
| # OR
Note: x <- 2 is an assignment
x < -2 is a comparison! Use extra spaces or parentheses
AND (&), OR (|) , NOT (!)
a b a & b
FALSE FALSE FALSE
FALSE TRUE FALSE
TRUE FALSE FALSE
TRUE TRUE TRUE
FALSE NA FALSE
TRUE NA NA
a b a | b
FALSE FALSE FALSE
FALSE TRUE TRUE
TRUE FALSE TRUE
TRUE TRUE TRUE
FALSE NA NA
TRUE NA TRUE
a ! a
FALSE TRUE
TRUE FALSE
NA NA
Auto-recycling of vector content
If you combine vectors of different length, R will automatically
‘recycle’ the content of the shortest vector to become the length of
the longest:
> mynumbers <- c(10.4, 5, 8.4, 3)
> mynumbers2 <- mynumbers + 1
> mynumbers2
11.4, 6, 9.4, 4 # In fact, mynumbers + c(1, 1, 1, 1) is done
But also:
> mynumbers2 + c(2, 30)
13.4, 36, 11.4, 34 # Here, mynumbers2 + c(2, 30, 2, 30) is done.
Recycling also works with logical operators
Comparison of equal length vectors (no recycling needed) :
> v1 <- c(10, 5, 5, 1)
> v2 <- c(10, 3, 5, 2)
> v1 == v2
TRUE, FALSE, TRUE, FALSE
Comparison of unequal length vectors:
> v1 == 5 # The value 5 is recycled to get an equal length vector.
# So in fact, v1 == c(5,5,5,5) is done
FALSE, TRUE, TRUE, FALSE
Operators
delA delB delC
geneA 1 2 3
geneB 4 5 6
> ind <- x["geneA", ] > 1 & x["geneA", ] < 3
> x["geneA", ind]
[1] 2
Combining logical operators
AND-operator has precedence over OR-operator
(like in mathematics: *, / have precedence over -, +)
Group them with parentheses if needed, or for clarity
> ind <- ( x < -1.7 | x > 2 ) & !is.na(x)
Select statements
Special (common) functions, all return a logical vector
is.na()
is.numeric() (and also is.character(), is.factor(), is.matrix(), is.data.frame() )
duplicated()
! # (exclamation mark): logical NOT, i.e. negation
Used a lot in checking the consistency of your data or arguments
for a function
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

11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng웅식 전
 
Cheat sheet python3
Cheat sheet python3Cheat sheet python3
Cheat sheet python3
sxw2k
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
Chu An
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheet
Gil Cohen
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
Rajendran
 
Python For Data Science Cheat Sheet
Python For Data Science Cheat SheetPython For Data Science Cheat Sheet
Python For Data Science Cheat Sheet
Karlijn Willems
 
Python_ 3 CheatSheet
Python_ 3 CheatSheetPython_ 3 CheatSheet
Python_ 3 CheatSheet
Dr. Volkan OBAN
 
Python programming -Tuple and Set Data type
Python programming -Tuple and Set Data typePython programming -Tuple and Set Data type
Python programming -Tuple and Set Data type
Megha V
 
Data Types and Structures in R
Data Types and Structures in RData Types and Structures in R
Data Types and Structures in R
Rupak Roy
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)
gekiaruj
 
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
 
Scikit-learn Cheatsheet-Python
Scikit-learn Cheatsheet-PythonScikit-learn Cheatsheet-Python
Scikit-learn Cheatsheet-Python
Dr. Volkan OBAN
 
Dplyr and Plyr
Dplyr and PlyrDplyr and Plyr
Dplyr and Plyr
Paul Richards
 
Introduction to R r.nabati - iausdj.ac.ir
Introduction to R   r.nabati - iausdj.ac.irIntroduction to R   r.nabati - iausdj.ac.ir
Introduction to R r.nabati - iausdj.ac.ir
nabati
 
Python Cheat Sheet
Python Cheat SheetPython Cheat Sheet
Python Cheat Sheet
Muthu Vinayagam
 
Data manipulation with dplyr
Data manipulation with dplyrData manipulation with dplyr
Data manipulation with dplyr
Romain Francois
 
Numpy python cheat_sheet
Numpy python cheat_sheetNumpy python cheat_sheet
Numpy python cheat_sheet
Nishant Upadhyay
 
Pandas Cheat Sheet
Pandas Cheat SheetPandas Cheat Sheet
Pandas Cheat Sheet
ACASH1011
 
Mementopython3 english
Mementopython3 englishMementopython3 english
Mementopython3 english
ssuser442080
 
Data Visualization using base graphics
Data Visualization using base graphicsData Visualization using base graphics
Data Visualization using base graphics
Rupak Roy
 

What's hot (20)

11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
 
Cheat sheet python3
Cheat sheet python3Cheat sheet python3
Cheat sheet python3
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheet
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Python For Data Science Cheat Sheet
Python For Data Science Cheat SheetPython For Data Science Cheat Sheet
Python For Data Science Cheat Sheet
 
Python_ 3 CheatSheet
Python_ 3 CheatSheetPython_ 3 CheatSheet
Python_ 3 CheatSheet
 
Python programming -Tuple and Set Data type
Python programming -Tuple and Set Data typePython programming -Tuple and Set Data type
Python programming -Tuple and Set Data type
 
Data Types and Structures in R
Data Types and Structures in RData Types and Structures in R
Data Types and Structures in R
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)
 
Manipulating Data using base R package
Manipulating Data using base R package Manipulating Data using base R package
Manipulating Data using base R package
 
Scikit-learn Cheatsheet-Python
Scikit-learn Cheatsheet-PythonScikit-learn Cheatsheet-Python
Scikit-learn Cheatsheet-Python
 
Dplyr and Plyr
Dplyr and PlyrDplyr and Plyr
Dplyr and Plyr
 
Introduction to R r.nabati - iausdj.ac.ir
Introduction to R   r.nabati - iausdj.ac.irIntroduction to R   r.nabati - iausdj.ac.ir
Introduction to R r.nabati - iausdj.ac.ir
 
Python Cheat Sheet
Python Cheat SheetPython Cheat Sheet
Python Cheat Sheet
 
Data manipulation with dplyr
Data manipulation with dplyrData manipulation with dplyr
Data manipulation with dplyr
 
Numpy python cheat_sheet
Numpy python cheat_sheetNumpy python cheat_sheet
Numpy python cheat_sheet
 
Pandas Cheat Sheet
Pandas Cheat SheetPandas Cheat Sheet
Pandas Cheat Sheet
 
Mementopython3 english
Mementopython3 englishMementopython3 english
Mementopython3 english
 
Data Visualization using base graphics
Data Visualization using base graphicsData Visualization using base graphics
Data Visualization using base graphics
 

Similar to Day 1c access, select ordering copy.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
 
[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2
Kevin Chun-Hsien Hsu
 
Vectors.pptx
Vectors.pptxVectors.pptx
Vectors.pptx
NivethithaM9
 
R Programming Homework Help
R Programming Homework HelpR Programming Homework Help
R Programming Homework Help
Statistics Homework Helper
 
R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
062MayankSinghal
 
Introduction to r
Introduction to rIntroduction to r
Introduction to r
Golden Julie Jesus
 
R Basics
R BasicsR Basics
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 programming language
R programming languageR programming language
R programming language
Alberto Minetti
 
Programming with matlab session 1
Programming with matlab session 1Programming with matlab session 1
Programming with matlab session 1
Infinity Tech Solutions
 
R Programming.pptx
R Programming.pptxR Programming.pptx
R Programming.pptx
kalai75
 
Vectors data frames
Vectors data framesVectors data frames
Vectors data frames
FAO
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd Esa
Mohd Esa
 
Programming in R
Programming in RProgramming in R
Programming in R
Smruti Sarangi
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
Avjinder (Avi) Kaler
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
Avjinder (Avi) Kaler
 
Matlab-1.pptx
Matlab-1.pptxMatlab-1.pptx
Matlab-1.pptx
aboma2hawi
 

Similar to Day 1c access, select ordering copy.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
 
[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2
 
Vectors.pptx
Vectors.pptxVectors.pptx
Vectors.pptx
 
bobok
bobokbobok
bobok
 
R Programming Homework Help
R Programming Homework HelpR Programming Homework Help
R Programming Homework Help
 
R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
 
Introduction to r
Introduction to rIntroduction to r
Introduction to r
 
R Basics
R BasicsR Basics
R Basics
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environment
 
R programming language
R programming languageR programming language
R programming language
 
Programming with matlab session 1
Programming with matlab session 1Programming with matlab session 1
Programming with matlab session 1
 
R Programming.pptx
R Programming.pptxR Programming.pptx
R Programming.pptx
 
Vectors data frames
Vectors data framesVectors data frames
Vectors data frames
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd Esa
 
Programming in R
Programming in RProgramming in R
Programming in R
 
R교육1
R교육1R교육1
R교육1
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
 
Matlab-1.pptx
Matlab-1.pptxMatlab-1.pptx
Matlab-1.pptx
 

Recently uploaded

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
 
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
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
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
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
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
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
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
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
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
 
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
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
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
 
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
 
"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
 

Recently uploaded (20)

A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
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...
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
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
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
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
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
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
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.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
 
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
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
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
 
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
 
"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...
 

Day 1c access, select ordering copy.pptx

  • 1. Access, select & ordering Day 1 - Introduction to R for Life Sciences
  • 2. Accessing vectors, matrices, data.frames Positions within vectors, matrices and data.frames are accessed using [ ]: > v <- c(10, 3, 5, 10) > v[2] 3 [] can also be used to assign (write) new values, e.g: v[2] <- 10 ( ) are used for function calls (or grouping operators, more later) !!! for instance: myvector <- c( ), mymatrix <- matrix( ), mydata <- data.frame( )
  • 3. Three ways to access values from vectors, matrices and data.frames Integers: specify the positions of the elements you mean Logical: specify (using TRUE/FALSE) which elements you want Character: specify their names only if your vector/matrix/data.frame has (unique) names! All these selections are made with vectors. They are sometimes called indexes.
  • 4. Examples: chromlength <- c(230218, 813184, 316620, 1531933) Integer: chromlength[ c(4, 2) ] => 1531933, 813184 Logical: chromlength[ c(FALSE, FALSE, TRUE, FALSE) ] => 316620 Character: names(chromlength) <- c("chrI", "chrII", "chrIII", "chrIV") chromlength[ c("chrIII", "chrI") ] => 316620, 230218
  • 5. Specifics for lists & data.frames lists >mylist <- list(analysis=”GSEA”, genes=c(“Foxo3a”, “TP53”), cutoff=0.05) > mylist$analysis > mylist$genes[2] data.frames > mydata[ , "id"] > mydata$id # does the same thing
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. 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 > x["geneA", x["geneA", ] > 1] # same as above, but implicit
  • 11. Operators < # Less than > # Greater than == # Equal to. Note: don’t confuse with = (assignment) >= # Greater than or equal to <= # Less than or equal to & # AND | # OR Note: x <- 2 is an assignment x < -2 is a comparison! Use extra spaces or parentheses
  • 12. AND (&), OR (|) , NOT (!) a b a & b FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE FALSE NA FALSE TRUE NA NA a b a | b FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE FALSE NA NA TRUE NA TRUE a ! a FALSE TRUE TRUE FALSE NA NA
  • 13. Auto-recycling of vector content If you combine vectors of different length, R will automatically ‘recycle’ the content of the shortest vector to become the length of the longest: > mynumbers <- c(10.4, 5, 8.4, 3) > mynumbers2 <- mynumbers + 1 > mynumbers2 11.4, 6, 9.4, 4 # In fact, mynumbers + c(1, 1, 1, 1) is done But also: > mynumbers2 + c(2, 30) 13.4, 36, 11.4, 34 # Here, mynumbers2 + c(2, 30, 2, 30) is done.
  • 14. Recycling also works with logical operators Comparison of equal length vectors (no recycling needed) : > v1 <- c(10, 5, 5, 1) > v2 <- c(10, 3, 5, 2) > v1 == v2 TRUE, FALSE, TRUE, FALSE Comparison of unequal length vectors: > v1 == 5 # The value 5 is recycled to get an equal length vector. # So in fact, v1 == c(5,5,5,5) is done FALSE, TRUE, TRUE, FALSE
  • 15. Operators delA delB delC geneA 1 2 3 geneB 4 5 6 > ind <- x["geneA", ] > 1 & x["geneA", ] < 3 > x["geneA", ind] [1] 2
  • 16. Combining logical operators AND-operator has precedence over OR-operator (like in mathematics: *, / have precedence over -, +) Group them with parentheses if needed, or for clarity > ind <- ( x < -1.7 | x > 2 ) & !is.na(x)
  • 17. Select statements Special (common) functions, all return a logical vector is.na() is.numeric() (and also is.character(), is.factor(), is.matrix(), is.data.frame() ) duplicated() ! # (exclamation mark): logical NOT, i.e. negation Used a lot in checking the consistency of your data or arguments for a function
  • 18. 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