SlideShare une entreprise Scribd logo
1  sur  18
Télécharger pour lire hors ligne
Introduction to R-Programming

             Paurush Praveen
        Algorithmic Bioinformatics
           BIT Research School

        praveen@bit.uni-bonn.de
Outline

 •   R as a new language
 •   R as a calculator
 •   Variables and Operators
 •   Data Structure
 •   Control Structure
 •   Functions
 •   String handle and regex

                       Paurush Praveen   2
R-Introduction

 A programming/scripting language widely
  used in statistical works
 An interpreted language, not a compiled
  one
 Free | Easy | Support | Compatibility
 Object oriented



                    Paurush Praveen         3
How does it work

 • variables, data, functions, results, etc, are
   stored in the active memory of the computer
   in the form of objects which have a name.

 • The user can do actions on these objects
   with operators (arithmetic, logical,
   comparison, ...) and functions (which are
   themselves objects).

 • The use of operators is relatively intuitive

                       Paurush Praveen             4
How does it look

 • command prompt symbol “>”




                                     Editor
      Console

                   Paurush Praveen            5
Input / Output

    •   Interactive session
    •   Input at the console itself
    •   input a script
    •   source(“codefile”)
    •   Usually R gives output on the command line.
    • To save as file use
    sink("file.txt") <Not for graphical output>
    ##return to the normal mode
    sink()

                            Paurush Praveen           6
Running R-programs

 • Save your commands in a file
   (viz. commands.R)
 • Call R on the command line:
   R commands.R
 • Call the script from within R:
   > commands.R



                       Paurush Praveen   7
R as a calculator

 • + - * / work as usual
 • pi etc. exist as Functions with brackets:
   sqrt(pi)
 • a call without paretheses gives the
   sourcecode
 • # denote comments
 • assignment by <-
 • library()
 • data()

                       Paurush Praveen         8
Operators




Binary operators work on vectors,
                                         Source: http://www.statmethods.net/management/operators.html
matrices and scalars

                                    Paurush Praveen                                                9
Data Types

  • A vector contains an indexed set of values
    that are all of the same type:
        • logical
        • numeric
        • complex
        • Character
       – The numeric type can be further broken down into
         integer, single, and double types (but this is only
         important when making calls to foreign functions, eg. C
         or Fortran.)




                             Paurush Praveen                       10
Data Structures

    • Data Structures
       –   vector - arrays of the same type
       –   factor - categorical
       –   list - can contain objects of different types
       –   matrix - table of numbers
       –   data.frame - table of numbers and/or characters
       –   environment
       –   hashtable
       –   function




                               Paurush Praveen               11
Control Structures

    for and while Loops
    The syntax for writing for loops in R is
    for(i in 1:N){
     Loop Code*
    }

    The syntax for while loops in R is
    while(logical argument){
    Loop Code*
    }

    Repeat {expression}

                     *where Loop Code should be substituted with the code you want to run in the loop.
                                         Paurush Praveen                                                 12
Control structures

    Conditional statements

     if (condition) true_expression else false_expression

     if (condition) expression

     if( condition 1) { statement1
            } else if(condition2) { statement2
                } else if(condition3) { statement3
           }




                                  Paurush Praveen            13
Writing Functions

 • Writing R functions provides a means of
   adding new functionality to the language
 • Functions that a user writes have the same
   status as those which are provided with R.
 • Reading the functions provided with the R
   system is a good way to learn how to write
   functions.


                      Paurush Praveen           14
Writing Functions

 • You can declare your own function in R using
   the function() command. The syntax is

         myfunction() <- function(input1,input2,..)
     {
           Function code
     }

 • Functions can be called with the arguments
         myfunction(argument1, argument2,...)



                                  Paurush Praveen     15
String Handle

 • Character strings are one of the basic types in R
 • A character vector is a vector of character strings,
   not characters
 • "" is an empty string
 • Special characters -> a backslash followed by a
   symbol for the special character
    – toupper(x) & tolower(x) : To convert case
    – chartr(old,new,x) Replaces any occurrence in x
    – sub and gsub for substitution


                               Paurush Praveen            16
Regular Expression

 • Regular Expression (Regex)
 • Properties
   – Can be combined
   – Can be used with metacharacters to define the
     pattern
 • Using regular expressions
   – grep (pattern,x)
   – grep (pattern, x, value=TRUE)



                               Paurush Praveen      17
Further References

 • Use ?<command> in console to seek help or
   help(<command>)
 • http://cran.r-project.org/doc/manuals/R-intro.html
 • http://www.cyclismo.org/tutorial/R/
 • Books
                         • Web helps can be more useful
                         • Google to search for specific problems
                         • http://www.rseek.org/ is a search
                           engine for R specific topics
                                Another Book "R in a Nutshell", Adler J, Oreilly




                        Paurush Praveen                                            18

Contenu connexe

Tendances

Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programming
Nimrita Koul
 
Presentation of OpenNLP
Presentation of OpenNLPPresentation of OpenNLP
Presentation of OpenNLP
Robert Viseur
 
Recursive descent parsing
Recursive descent parsingRecursive descent parsing
Recursive descent parsing
Boy Baukema
 
Prolog (present)
Prolog (present) Prolog (present)
Prolog (present)
Melody Joey
 

Tendances (20)

Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programming
 
Unit 4
Unit 4Unit 4
Unit 4
 
Python Data Types
Python Data TypesPython Data Types
Python Data Types
 
Lisp and scheme i
Lisp and scheme iLisp and scheme i
Lisp and scheme i
 
ProLog (Artificial Intelligence) Introduction
ProLog (Artificial Intelligence) IntroductionProLog (Artificial Intelligence) Introduction
ProLog (Artificial Intelligence) Introduction
 
Prolog 7-Languages
Prolog 7-LanguagesProlog 7-Languages
Prolog 7-Languages
 
Presentation of OpenNLP
Presentation of OpenNLPPresentation of OpenNLP
Presentation of OpenNLP
 
The Scheme Language -- Using it on the iPhone
The Scheme Language -- Using it on the iPhoneThe Scheme Language -- Using it on the iPhone
The Scheme Language -- Using it on the iPhone
 
Lexical analysis
Lexical analysisLexical analysis
Lexical analysis
 
Recursive Descent Parsing
Recursive Descent Parsing  Recursive Descent Parsing
Recursive Descent Parsing
 
Getting Started with R
Getting Started with RGetting Started with R
Getting Started with R
 
PROLOG: Introduction To Prolog
PROLOG: Introduction To PrologPROLOG: Introduction To Prolog
PROLOG: Introduction To Prolog
 
Data types & object
Data types & objectData types & object
Data types & object
 
Recursive descent parsing
Recursive descent parsingRecursive descent parsing
Recursive descent parsing
 
Aaa ped-2- Python: Basics
Aaa ped-2- Python: BasicsAaa ped-2- Python: Basics
Aaa ped-2- Python: Basics
 
Prolog (present)
Prolog (present) Prolog (present)
Prolog (present)
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
 
Introduction to data analysis using R
Introduction to data analysis using RIntroduction to data analysis using R
Introduction to data analysis using R
 

En vedette

숙명여대 09 심한
숙명여대 09 심한숙명여대 09 심한
숙명여대 09 심한
hgucontents
 
Qlda 6-khoanngoai[easyvn.net]
Qlda 6-khoanngoai[easyvn.net]Qlda 6-khoanngoai[easyvn.net]
Qlda 6-khoanngoai[easyvn.net]
huongntt16
 
CtrlS: Cloud Solutions for Retail & eCommerce
CtrlS: Cloud Solutions for Retail & eCommerceCtrlS: Cloud Solutions for Retail & eCommerce
CtrlS: Cloud Solutions for Retail & eCommerce
eTailing India
 
Tarun Arora- ATOM- eTailing India Conclave Jaipur- 2013
Tarun Arora- ATOM- eTailing India Conclave Jaipur- 2013Tarun Arora- ATOM- eTailing India Conclave Jaipur- 2013
Tarun Arora- ATOM- eTailing India Conclave Jaipur- 2013
eTailing India
 
Mauritius mandla
Mauritius mandlaMauritius mandla
Mauritius mandla
Mandla Dube
 
Kdqt eng chap002
Kdqt eng chap002Kdqt eng chap002
Kdqt eng chap002
huongntt16
 
– RathinLahiri:What are the things you can do take advantage of Digital Chann...
– RathinLahiri:What are the things you can do take advantage of Digital Chann...– RathinLahiri:What are the things you can do take advantage of Digital Chann...
– RathinLahiri:What are the things you can do take advantage of Digital Chann...
eTailing India
 
Joanmateu fiem pac1
Joanmateu fiem pac1Joanmateu fiem pac1
Joanmateu fiem pac1
decosigma
 

En vedette (19)

Rasheed resumere 2013
Rasheed resumere 2013Rasheed resumere 2013
Rasheed resumere 2013
 
New channel distributions unlock retail india
New channel distributions unlock retail indiaNew channel distributions unlock retail india
New channel distributions unlock retail india
 
숙명여대 09 심한
숙명여대 09 심한숙명여대 09 심한
숙명여대 09 심한
 
dene/ders1.ppt
dene/ders1.pptdene/ders1.ppt
dene/ders1.ppt
 
CCI Media event promotion and controlling
CCI Media event promotion and controllingCCI Media event promotion and controlling
CCI Media event promotion and controlling
 
Women show the way in india’s progress
Women show the way in india’s progressWomen show the way in india’s progress
Women show the way in india’s progress
 
Qlda 6-khoanngoai[easyvn.net]
Qlda 6-khoanngoai[easyvn.net]Qlda 6-khoanngoai[easyvn.net]
Qlda 6-khoanngoai[easyvn.net]
 
CtrlS: Cloud Solutions for Retail & eCommerce
CtrlS: Cloud Solutions for Retail & eCommerceCtrlS: Cloud Solutions for Retail & eCommerce
CtrlS: Cloud Solutions for Retail & eCommerce
 
Tarun Arora- ATOM- eTailing India Conclave Jaipur- 2013
Tarun Arora- ATOM- eTailing India Conclave Jaipur- 2013Tarun Arora- ATOM- eTailing India Conclave Jaipur- 2013
Tarun Arora- ATOM- eTailing India Conclave Jaipur- 2013
 
Mauritius mandla
Mauritius mandlaMauritius mandla
Mauritius mandla
 
Egiptulantic
EgiptulanticEgiptulantic
Egiptulantic
 
Kdqt eng chap002
Kdqt eng chap002Kdqt eng chap002
Kdqt eng chap002
 
– RathinLahiri:What are the things you can do take advantage of Digital Chann...
– RathinLahiri:What are the things you can do take advantage of Digital Chann...– RathinLahiri:What are the things you can do take advantage of Digital Chann...
– RathinLahiri:What are the things you can do take advantage of Digital Chann...
 
How to Use Blogs for Lead Generation
How to Use Blogs for Lead GenerationHow to Use Blogs for Lead Generation
How to Use Blogs for Lead Generation
 
Joanmateu fiem pac1
Joanmateu fiem pac1Joanmateu fiem pac1
Joanmateu fiem pac1
 
Former tcs chief's resignation from government posts sparks buzz
Former tcs chief's resignation from government posts sparks buzzFormer tcs chief's resignation from government posts sparks buzz
Former tcs chief's resignation from government posts sparks buzz
 
Web service transition. From desktop to mobile
Web service transition. From desktop to mobileWeb service transition. From desktop to mobile
Web service transition. From desktop to mobile
 
Global Firms Eye On India’s eCommerce Pie
Global Firms Eye On India’s eCommerce PieGlobal Firms Eye On India’s eCommerce Pie
Global Firms Eye On India’s eCommerce Pie
 
National Philanthropy Day in CT 2013
National Philanthropy Day in CT 2013National Philanthropy Day in CT 2013
National Philanthropy Day in CT 2013
 

Similaire à Introduction r-programming

Slides on introduction to R by ArinBasu MD
Slides on introduction to R by ArinBasu MDSlides on introduction to R by ArinBasu MD
Slides on introduction to R by ArinBasu MD
SonaCharles2
 

Similaire à Introduction r-programming (20)

محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
محاضرة برنامج التحليل الكمي   R program د.هديل القفيديمحاضرة برنامج التحليل الكمي   R program د.هديل القفيدي
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
 
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
محاضرة برنامج التحليل الكمي   R program د.هديل القفيديمحاضرة برنامج التحليل الكمي   R program د.هديل القفيدي
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
 
R - Get Started I - Sanaitics
R - Get Started I - SanaiticsR - Get Started I - Sanaitics
R - Get Started I - Sanaitics
 
Introduction to R software, by Leire ibaibarriaga
Introduction to R software, by Leire ibaibarriaga Introduction to R software, by Leire ibaibarriaga
Introduction to R software, by Leire ibaibarriaga
 
Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014
 
Big data analytics with R tool.pptx
Big data analytics with R tool.pptxBig data analytics with R tool.pptx
Big data analytics with R tool.pptx
 
1_Introduction.pptx
1_Introduction.pptx1_Introduction.pptx
1_Introduction.pptx
 
Advanced Data Analytics with R Programming.ppt
Advanced Data Analytics with R Programming.pptAdvanced Data Analytics with R Programming.ppt
Advanced Data Analytics with R Programming.ppt
 
R Programming Language
R Programming LanguageR Programming Language
R Programming Language
 
17641.ppt
17641.ppt17641.ppt
17641.ppt
 
Slides on introduction to R by ArinBasu MD
Slides on introduction to R by ArinBasu MDSlides on introduction to R by ArinBasu MD
Slides on introduction to R by ArinBasu MD
 
17641.ppt
17641.ppt17641.ppt
17641.ppt
 
Python
PythonPython
Python
 
How to obtain and install R.ppt
How to obtain and install R.pptHow to obtain and install R.ppt
How to obtain and install R.ppt
 
R basics for MBA Students[1].pptx
R basics for MBA Students[1].pptxR basics for MBA Students[1].pptx
R basics for MBA Students[1].pptx
 
Bioinformatics v2014 wim_vancriekinge
Bioinformatics v2014 wim_vancriekingeBioinformatics v2014 wim_vancriekinge
Bioinformatics v2014 wim_vancriekinge
 
WiNGS 2014 Workshop 2 R, RStudio, and reproducible research with knitr
WiNGS 2014 Workshop 2 R, RStudio, and reproducible research with knitrWiNGS 2014 Workshop 2 R, RStudio, and reproducible research with knitr
WiNGS 2014 Workshop 2 R, RStudio, and reproducible research with knitr
 
Intro to data science module 1 r
Intro to data science module 1 rIntro to data science module 1 r
Intro to data science module 1 r
 
Python ppt
Python pptPython ppt
Python ppt
 
R training
R trainingR training
R training
 

Introduction r-programming

  • 1. Introduction to R-Programming Paurush Praveen Algorithmic Bioinformatics BIT Research School praveen@bit.uni-bonn.de
  • 2. Outline • R as a new language • R as a calculator • Variables and Operators • Data Structure • Control Structure • Functions • String handle and regex Paurush Praveen 2
  • 3. R-Introduction A programming/scripting language widely used in statistical works An interpreted language, not a compiled one Free | Easy | Support | Compatibility Object oriented Paurush Praveen 3
  • 4. How does it work • variables, data, functions, results, etc, are stored in the active memory of the computer in the form of objects which have a name. • The user can do actions on these objects with operators (arithmetic, logical, comparison, ...) and functions (which are themselves objects). • The use of operators is relatively intuitive Paurush Praveen 4
  • 5. How does it look • command prompt symbol “>” Editor Console Paurush Praveen 5
  • 6. Input / Output • Interactive session • Input at the console itself • input a script • source(“codefile”) • Usually R gives output on the command line. • To save as file use sink("file.txt") <Not for graphical output> ##return to the normal mode sink() Paurush Praveen 6
  • 7. Running R-programs • Save your commands in a file (viz. commands.R) • Call R on the command line: R commands.R • Call the script from within R: > commands.R Paurush Praveen 7
  • 8. R as a calculator • + - * / work as usual • pi etc. exist as Functions with brackets: sqrt(pi) • a call without paretheses gives the sourcecode • # denote comments • assignment by <- • library() • data() Paurush Praveen 8
  • 9. Operators Binary operators work on vectors, Source: http://www.statmethods.net/management/operators.html matrices and scalars Paurush Praveen 9
  • 10. Data Types • A vector contains an indexed set of values that are all of the same type: • logical • numeric • complex • Character – The numeric type can be further broken down into integer, single, and double types (but this is only important when making calls to foreign functions, eg. C or Fortran.) Paurush Praveen 10
  • 11. Data Structures • Data Structures – vector - arrays of the same type – factor - categorical – list - can contain objects of different types – matrix - table of numbers – data.frame - table of numbers and/or characters – environment – hashtable – function Paurush Praveen 11
  • 12. Control Structures for and while Loops The syntax for writing for loops in R is for(i in 1:N){ Loop Code* } The syntax for while loops in R is while(logical argument){ Loop Code* } Repeat {expression} *where Loop Code should be substituted with the code you want to run in the loop. Paurush Praveen 12
  • 13. Control structures Conditional statements  if (condition) true_expression else false_expression  if (condition) expression  if( condition 1) { statement1 } else if(condition2) { statement2 } else if(condition3) { statement3 } Paurush Praveen 13
  • 14. Writing Functions • Writing R functions provides a means of adding new functionality to the language • Functions that a user writes have the same status as those which are provided with R. • Reading the functions provided with the R system is a good way to learn how to write functions. Paurush Praveen 14
  • 15. Writing Functions • You can declare your own function in R using the function() command. The syntax is myfunction() <- function(input1,input2,..) { Function code } • Functions can be called with the arguments myfunction(argument1, argument2,...) Paurush Praveen 15
  • 16. String Handle • Character strings are one of the basic types in R • A character vector is a vector of character strings, not characters • "" is an empty string • Special characters -> a backslash followed by a symbol for the special character – toupper(x) & tolower(x) : To convert case – chartr(old,new,x) Replaces any occurrence in x – sub and gsub for substitution Paurush Praveen 16
  • 17. Regular Expression • Regular Expression (Regex) • Properties – Can be combined – Can be used with metacharacters to define the pattern • Using regular expressions – grep (pattern,x) – grep (pattern, x, value=TRUE) Paurush Praveen 17
  • 18. Further References • Use ?<command> in console to seek help or help(<command>) • http://cran.r-project.org/doc/manuals/R-intro.html • http://www.cyclismo.org/tutorial/R/ • Books • Web helps can be more useful • Google to search for specific problems • http://www.rseek.org/ is a search engine for R specific topics Another Book "R in a Nutshell", Adler J, Oreilly Paurush Praveen 18