A few tricks on Matrices (1) The indices can still be give - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

A few tricks on Matrices (1) The indices can still be give

Description:

A few tricks on Matrices (1) The indices can still be given as a vector ! ... More tricks. How many numbers in a matrix are equal to 5 ? ... – PowerPoint PPT presentation

Number of Views:85
Avg rating:3.0/5.0
Slides: 14
Provided by: man77
Category:
Tags: few | give | indices | matrices | still | tricks

less

Transcript and Presenter's Notes

Title: A few tricks on Matrices (1) The indices can still be give


1
An Introduction to R
Logic Basics
2
The R language
  • Command line
  • Can be executed within a terminal
  • Within Emacs using ESS (Emacs Speaks Statistics)

3
Data structures
  • Vector ( 1 dimensional array)
  • gt vect c(4,6,7,10)
  • gt vect2 c(vect,vect)
  • Matrix ( 2 dimensional array)
  • gt mat matrix(0, 2, 2) matrix of size 2x2,
    with 0
  • gt mat matrix(vect, 2, 2) matrix of size 2x2
  • Array ( n dimensional)
  • gt array array(0, dimc(10,10,10)) cube of
    10x10x10

These data structures settle the R logic as all
is designed to make an easy use of it
4
A few tricks on Vectors
  • The indices can be a vector ! Indices begin at
    1!!!
  • gt vect.A c(4,6,7,10)
  • gt vect.B c(1,4)
  • gt vect.A1
  • 4
  • gt vect.Avect.B equivalent to
    vect.Ac(1,4)
  • 4 10
  • The WHICH() function is the most useful
  • gt which(vect.A 7)3
  • gt which(vect.A gt 6)
  • 3 4
  • The , seq and gl functions allow to
    generate sequence of numbers

5
A few tricks on Matrices (1)
  • The indices can still be given as a vector !
  • gt MY.matrixi,j give the element at line i,
    col j
  • gt MY.matrixi, gives the line i
  • gt MY.matrixc(1,2,3), gives the first 3 lines
    as a matrix
  • gt MY.matrixc(1,2,3), c(1,2,3) gives a
    sub-matrix

j
i
i
6
A few tricks on Matrices (2)
  • The WHICH() function is still very useful
  • Prints extreme values of a matrix
  • gt MY.matrixwhich(MY.matrix gt cutoff)here
    Values gt to cutoff are printed
  • An example
  • I have a file of the following type
  • pdb NB_chains NB_identical_int
    NB_homologous_int NB_different_int
  • gt data read.table(file /elevy/... )
  • gt identical.1 which(data,3 1)
  • gt dimers which(data,2 2)
  • gt homodimers intersect(identical,dimers)
  • gt datahomodimers, prints all the homodimer
    lines !

7
More tricks
  • How many numbers in a matrix are equal to 5 ?
  • How many numbers are in common between 2 matrices
    ?
  • Replace all the 4 by -4 in any data structure ?

8
Some useful functions (1)
  • Play with data structures
  • min, max, which.min, which.max
  • combined with sum
  • mean, sd
  • intersect
  • cor combined with hclust / heatmap
  • Cast operator as.type

9
Some useful functions (2)
  • For text printing
  • print(Hello)
  • print(paste(one, i , two , j ,sep ))

10
Some useful functions (3)
  • For statistical analysis
  • rand / random doesnt exists ! Their are
    specific laws instead
  • ? runif(x) Uniform law (equiv. To rand)
  • ? rnorm(x) Gaussian law

11
Some useful functions (4)
  • Useful graphical functions
  • Plot 2D look at demo(graphics)
  • Image 2D look at demo(image)
  • Heatmap (clust image tree)
  • par() store most of the graphical parameters to
    custom the display
  • Persp 3D look at demo(persp)
  • Find help examples
  • help.start() or help(function) or ?function

12
Some remarks
  • No  underscores  in variables names are allowed
  • (the dot is generally used instead)
  • The  dot  doesnt mean  method call  like in
    object oriented languages!
  • There is actually another  vector like data
    structure list
  • which allows to store objects rather than digits.
  • There is actually another  matrix like  data
    structure data.frame which is a matrix for
    which rows/columns can be given a name

13
Last remarks
  • You can run scripts in BATCH mode, example
  • R --vanilla lt script.r
  • To quit R, type q()
  • The () are very important, when you dont put it
    the source code of the function is printed! (this
    is true for any function)
  • Dont hesitate to ask questions ?
Write a Comment
User Comments (0)
About PowerShow.com