SlideShare a Scribd company logo
1 of 41
Learning R via Python
...or the other way around


       Drew Conway

      Dept. of Politics - NYU



      January 7, 2010
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


What We’ll Cover



        Brief review of Python
               The Zen of Python
               How are R and Python the same, and how are they different
        Similar Data Structures
               Python dictionary
               R list
        Example - Testing if an integer is prime
               Create a function and returning a value
               Using while-loops
        Bridging the gap with RPy2
               Running a regression with R inside Python
        Python resources




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Fundamental Elements of Python

        The Python coder’s creed
        >>> import this
        The Zen of Python, by Tim Peters

        Beautiful is better than ugly.
        Explicit is better than implicit.
        Simple is better than complex.
        Complex is better than complicated.
        Flat is better than nested.
        Sparse is better than dense.
        Readability counts.
        Special cases aren’t special enough to break the rules.
        Although practicality beats purity.
        Errors should never pass silently.
        Unless explicitly silenced.
        In the face of ambiguity, refuse the temptation to guess.
        There should be one-- and preferably only one --obvious way to do it.
        Although that way may not be obvious at first unless you’re Dutch.
        Now is better than never.
        Although never is often better than *right* now.
        If the implementation is hard to explain, it’s a bad idea.
        If the implementation is easy to explain, it may be a good idea.
        Namespaces are one honking great idea -- let’s do more of those!



                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


How are R and Python alike, and how are they different?

  Similarities                                                          Differences




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


How are R and Python alike, and how are they different?

  Similarities                                                          Differences
  Functional programming paradigm

  Array of squared values
  # In Python we use a lambda nested in map
  >>> map(lambda x: x**2,range(1,11))
  [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


How are R and Python alike, and how are they different?

  Similarities                                                          Differences
  Functional programming paradigm

  Array of squared values
  # In Python we use a lambda nested in map
  >>> map(lambda x: x**2,range(1,11))
  [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
  # In R we define a function in sapply
  > sapply(1:10,function(x){return(x**2)})
   [1]   1   4   9 16 25 36 49 64 81 100




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


How are R and Python alike, and how are they different?

  Similarities                                                          Differences
  Functional programming paradigm

  Array of squared values
  # In Python we use a lambda nested in map
  >>> map(lambda x: x**2,range(1,11))
  [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
  # In R we define a function in sapply
  > sapply(1:10,function(x){return(x**2)})
   [1]   1   4   9 16 25 36 49 64 81 100


  Object-oriented programming
      Both languages support robust class
      structures
          In R, there are the S3 and S4 classes




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


How are R and Python alike, and how are they different?

  Similarities                                                          Differences
  Functional programming paradigm

  Array of squared values
  # In Python we use a lambda nested in map
  >>> map(lambda x: x**2,range(1,11))
  [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
  # In R we define a function in sapply
  > sapply(1:10,function(x){return(x**2)})
   [1]   1   4   9 16 25 36 49 64 81 100


  Object-oriented programming
      Both languages support robust class
      structures
        In R, there are the S3 and S4 classes
  Easily call C/Fortran code
        Due to their high-level, both languages
        provide functionality to call compiled code
        from lower-level languages




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


How are R and Python alike, and how are they different?

  Similarities                                                          Differences
  Functional programming paradigm                                       General purpose OOP vs. Functional OOP
                                                                            Python is a high-level language for
  Array of squared values
                                                                            application development
  # In Python we use a lambda nested in map
  >>> map(lambda x: x**2,range(1,11))                                           R is a collection of functions for data
  [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]                                        analysis
  # In R we define a function in sapply
  > sapply(1:10,function(x){return(x**2)})
   [1]   1   4   9 16 25 36 49 64 81 100


  Object-oriented programming
      Both languages support robust class
      structures
        In R, there are the S3 and S4 classes
  Easily call C/Fortran code
        Due to their high-level, both languages
        provide functionality to call compiled code
        from lower-level languages




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


How are R and Python alike, and how are they different?

  Similarities                                                          Differences
  Functional programming paradigm                                       General purpose OOP vs. Functional OOP
                                                                            Python is a high-level language for
  Array of squared values
                                                                            application development
  # In Python we use a lambda nested in map
  >>> map(lambda x: x**2,range(1,11))                                        R is a collection of functions for data
  [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]                                     analysis
  # In R we define a function in sapply                                 Syntax structure
  > sapply(1:10,function(x){return(x**2)})
   [1]   1   4   9 16 25 36 49 64 81 100                                     Python syntax forces readability
                                                                             through whitespace
                                                                                R emphasizes parsimony and nesting
  Object-oriented programming
      Both languages support robust class
      structures
        In R, there are the S3 and S4 classes
  Easily call C/Fortran code
        Due to their high-level, both languages
        provide functionality to call compiled code
        from lower-level languages




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


How are R and Python alike, and how are they different?

  Similarities                                                          Differences
  Functional programming paradigm                                       General purpose OOP vs. Functional OOP
                                                                            Python is a high-level language for
  Array of squared values
                                                                            application development
  # In Python we use a lambda nested in map
  >>> map(lambda x: x**2,range(1,11))                                        R is a collection of functions for data
  [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]                                     analysis
  # In R we define a function in sapply                                 Syntax structure
  > sapply(1:10,function(x){return(x**2)})
   [1]   1   4   9 16 25 36 49 64 81 100                                     Python syntax forces readability
                                                                             through whitespace
                                                                                R emphasizes parsimony and nesting
  Object-oriented programming
      Both languages support robust class                               Creating a function
      structures
        In R, there are the S3 and S4 classes                           # In Python functions are declared
                                                                        >>> def my func(x):
  Easily call C/Fortran code                                            ...
        Due to their high-level, both languages
        provide functionality to call compiled code
        from lower-level languages




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


How are R and Python alike, and how are they different?

  Similarities                                                          Differences
  Functional programming paradigm                                       General purpose OOP vs. Functional OOP
                                                                            Python is a high-level language for
  Array of squared values
                                                                            application development
  # In Python we use a lambda nested in map
  >>> map(lambda x: x**2,range(1,11))                                        R is a collection of functions for data
  [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]                                     analysis
  # In R we define a function in sapply                                 Syntax structure
  > sapply(1:10,function(x){return(x**2)})
   [1]   1   4   9 16 25 36 49 64 81 100                                     Python syntax forces readability
                                                                             through whitespace
                                                                                R emphasizes parsimony and nesting
  Object-oriented programming
      Both languages support robust class                               Creating a function
      structures
        In R, there are the S3 and S4 classes                           # In Python functions are declared
                                                                        >>> def my func(x):
  Easily call C/Fortran code                                            ...
        Due to their high-level, both languages                         # In R functions are assigned
                                                                        > my.func<-function(x) { ... }
        provide functionality to call compiled code
        from lower-level languages




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


How are R and Python alike, and how are they different?

  Similarities                                                          Differences
  Functional programming paradigm                                       General purpose OOP vs. Functional OOP
                                                                            Python is a high-level language for
  Array of squared values
                                                                            application development
  # In Python we use a lambda nested in map
  >>> map(lambda x: x**2,range(1,11))                                        R is a collection of functions for data
  [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]                                     analysis
  # In R we define a function in sapply                                 Syntax structure
  > sapply(1:10,function(x){return(x**2)})
   [1]   1   4   9 16 25 36 49 64 81 100                                     Python syntax forces readability
                                                                             through whitespace
                                                                                R emphasizes parsimony and nesting
  Object-oriented programming
      Both languages support robust class                               Creating a function
      structures
        In R, there are the S3 and S4 classes                           # In Python functions are declared
                                                                        >>> def my func(x):
  Easily call C/Fortran code                                            ...
        Due to their high-level, both languages                         # In R functions are assigned
                                                                        > my.func<-function(x) { ... }
        provide functionality to call compiled code
        from lower-level languages
         For more info see: StackOverlow.com discussion on topic


                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Python Dictionaries

        In Python the dict type is a data structure that represents a key→value
        mapping




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Python Dictionaries

        In Python the dict type is a data structure that represents a key→value
        mapping

        Working with the dict type
        # Keys and values can be of any data type
        >>> fruit dict={"apple":1,"orange":[0.23,0.11],"banana":True }




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Python Dictionaries

        In Python the dict type is a data structure that represents a key→value
        mapping

        Working with the dict type
        # Keys and values can be of any data type
        >>> fruit dict={"apple":1,"orange":[0.23,0.11],"banana":True }

        # Can retrieve the keys and values as Python lists (vector)
        >>> fruit dict.keys()
        ["orange","apple","banana"]




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Python Dictionaries

        In Python the dict type is a data structure that represents a key→value
        mapping

        Working with the dict type
        # Keys and values can be of any data type
        >>> fruit dict={"apple":1,"orange":[0.23,0.11],"banana":True }

        # Can retrieve the keys and values as Python lists (vector)
        >>> fruit dict.keys()
        ["orange","apple","banana"]

        # Or create a (key,value) tuple
        >>> fruit dict.items()
        [("orange",[0.23,0.11]),("apple",1),("Banana",True)]




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Python Dictionaries

        In Python the dict type is a data structure that represents a key→value
        mapping

        Working with the dict type
        # Keys and values can be of any data type
        >>> fruit dict={"apple":1,"orange":[0.23,0.11],"banana":True }

        # Can retrieve the keys and values as Python lists (vector)
        >>> fruit dict.keys()
        ["orange","apple","banana"]

        # Or create a (key,value) tuple
        >>> fruit dict.items()
        [("orange",[0.23,0.11]),("apple",1),("Banana",True)]
        # This becomes especially useful when you master Python ‘‘list comprehension’’




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Python Dictionaries

        In Python the dict type is a data structure that represents a key→value
        mapping

        Working with the dict type
        # Keys and values can be of any data type
        >>> fruit dict={"apple":1,"orange":[0.23,0.11],"banana":True }

        # Can retrieve the keys and values as Python lists (vector)
        >>> fruit dict.keys()
        ["orange","apple","banana"]

        # Or create a (key,value) tuple
        >>> fruit dict.items()
        [("orange",[0.23,0.11]),("apple",1),("Banana",True)]
        # This becomes especially useful when you master Python ‘‘list comprehension’’



        The Python dictionary is an extremely flexible and useful data structure,
        making it one of the primary advantages of Python over other languages
               Luckily, there is a fairly direct mapping between the Python dict and R
               lists!


                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


R Lists
        Similarly to the Python dictionary, the R list is a key→value mapping




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


R Lists
        Similarly to the Python dictionary, the R list is a key→value mapping

        Working with an R list
        > fruit.list<-list("apple"=1,"orange"=c(0.23,0.11),"banana"=TRUE)
        > fruit.list
        $apple
        [1] 1

        $orange
        [1] 0.55 0.11

        $banana
        [1] TRUE




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


R Lists
        Similarly to the Python dictionary, the R list is a key→value mapping

        Working with an R list
        > fruit.list<-list("apple"=1,"orange"=c(0.23,0.11),"banana"=TRUE)
        > fruit.list
        $apple
        [1] 1

        $orange
        [1] 0.55 0.11

        $banana
        [1] TRUE


        Notice, however, that R will always treat the value of the key/value pair as a vector;
        unlike Python, which does not care the value’s data type. Of the many R
        programming paradigms, the “vectorization of all data” is among the strongest.




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


R Lists
        Similarly to the Python dictionary, the R list is a key→value mapping

        Working with an R list
        > fruit.list<-list("apple"=1,"orange"=c(0.23,0.11),"banana"=TRUE)
        > fruit.list
        $apple
        [1] 1

        $orange
        [1] 0.55 0.11

        $banana
        [1] TRUE


        Notice, however, that R will always treat the value of the key/value pair as a vector;
        unlike Python, which does not care the value’s data type. Of the many R
        programming paradigms, the “vectorization of all data” is among the strongest.

        Using unlist
        > unlist(fruist.list)
        apple orange1 orange2          banana
         1.00    0.55    0.11            1.00



                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Testing a Prime in Python

        We are interested in testing whether integer X is prime, and to do so we will:
               Declare a function
               Use a while-loop
               Return a boolean




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Testing a Prime in Python

        We are interested in testing whether integer X is prime, and to do so we will:
               Declare a function
               Use a while-loop
               Return a boolean

        The is prime function
        def is prime(x):
            if x%2 == 0:
                return False
            else:
                y=3
                while y<x:
                    if x%y==0:
                         return False
                    else:
                         y+=2
                return True




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Testing a Prime in Python

        We are interested in testing whether integer X is prime, and to do so we will:
               Declare a function
               Use a while-loop
               Return a boolean

        The is prime function
        def is prime(x):
            if x%2 == 0:
                return False
            else:
                y=3
                while y<x:
                    if x%y==0:
                         return False
                    else:
                         y+=2
                return True


        Notice the number of lines in this simple function. In contrast to R, and many other
        programming languages, Python’s whitespace and indentation requirements force
        verbose code; however, the end result is very readable.

                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Testing a Prime in R


        Unlike Python where functions are declared explicitly, R creates functions through
        assignment. In many ways, this is somewhere between the Python lambda function
        and an explicit function declaration.




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Testing a Prime in R


        Unlike Python where functions are declared explicitly, R creates functions through
        assignment. In many ways, this is somewhere between the Python lambda function
        and an explicit function declaration.

        The is.prime function
        is.prime<-function(x) {
            if(x%%2==0) {return(FALSE)
            } else {
                y<-3
                while(y<x) ifelse(x%%y==0,return(FALSE),y<-y+2) }
            return(TRUE)
        }




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Testing a Prime in R


        Unlike Python where functions are declared explicitly, R creates functions through
        assignment. In many ways, this is somewhere between the Python lambda function
        and an explicit function declaration.

        The is.prime function
        is.prime<-function(x) {
            if(x%%2==0) {return(FALSE)
            } else {
                y<-3
                while(y<x) ifelse(x%%y==0,return(FALSE),y<-y+2) }
            return(TRUE)
        }


        Contrast the width of this version of the function with the length of the previous
            With the ifelse function R allows you to compress many lines of code
               A good example of the difference between a largely function programming
               language with objects (R) vs. a largely objected-oriented language with a core
               set of functions (Python)




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Bridging the Gap with RPy2

        Python is widely adopted within the scientific and data communities
               The combination of SciPy/NumPy/matplotlib represents a powerful set of
               tools for scientific computing
               Companies like Enthought are focused on servicing this area




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Bridging the Gap with RPy2

        Python is widely adopted within the scientific and data communities
               The combination of SciPy/NumPy/matplotlib represents a powerful set of
               tools for scientific computing
               Companies like Enthought are focused on servicing this area
        There are, however, many times when even this combination cannot match the
        analytical power of R—enter RPy2
               RPy2 is a Python package that allows you to call R directly from within a
               Python script
               This is particularly useful if you want to use one of R’s base functions
               inside Python




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Bridging the Gap with RPy2

        Python is widely adopted within the scientific and data communities
               The combination of SciPy/NumPy/matplotlib represents a powerful set of
               tools for scientific computing
               Companies like Enthought are focused on servicing this area
        There are, however, many times when even this combination cannot match the
        analytical power of R—enter RPy2
               RPy2 is a Python package that allows you to call R directly from within a
               Python script
               This is particularly useful if you want to use one of R’s base functions
               inside Python
        For example, creating a function to mimic R’s lm in Python—even using the
        above combination—would be very time consuming. To avoid this, we will use
        RPy2 to call lm from within a Python script and plot the data.




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Bridging the Gap with RPy2

        Python is widely adopted within the scientific and data communities
               The combination of SciPy/NumPy/matplotlib represents a powerful set of
               tools for scientific computing
               Companies like Enthought are focused on servicing this area
        There are, however, many times when even this combination cannot match the
        analytical power of R—enter RPy2
               RPy2 is a Python package that allows you to call R directly from within a
               Python script
               This is particularly useful if you want to use one of R’s base functions
               inside Python
        For example, creating a function to mimic R’s lm in Python—even using the
        above combination—would be very time consuming. To avoid this, we will use
        RPy2 to call lm from within a Python script and plot the data.



        Disclaimer: the syntax can be a bit tricky to understand at first, so allow for a
        fairly steep learning curve!

                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Calling lm from Python with RPy2


        Creating a linear model in Python




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Calling lm from Python with RPy2


        Creating a linear model in Python
        # First import RPy2 and create an instance of robjects
        import rpy2.robjects as robjects

        r = robjects.r




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Calling lm from Python with RPy2


        Creating a linear model in Python
        # First import RPy2 and create an instance of robjects
        import rpy2.robjects as robjects

        r = robjects.r
        # Create the data by passing a Python list to RPy2, which interprets as an R vector
        ctl = robjects.FloatVector([4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14])
        trt = robjects.FloatVector([4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69])
        group = r.gl(2, 10, 20, labels = ["Ctl","Trt"])
        weight = ctl + trt




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Calling lm from Python with RPy2


        Creating a linear model in Python
        # First import RPy2 and create an instance of robjects
        import rpy2.robjects as robjects

        r = robjects.r
        # Create the data by passing a Python list to RPy2, which interprets as an R vector
        ctl = robjects.FloatVector([4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14])
        trt = robjects.FloatVector([4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69])
        group = r.gl(2, 10, 20, labels = ["Ctl","Trt"])
        weight = ctl + trt
        # RPy2 uses Python dictionary types to index data
        robjects.globalEnv["weight"] = weight
        robjects.globalEnv["group"] = group




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Calling lm from Python with RPy2


        Creating a linear model in Python
        # First import RPy2 and create an instance of robjects
        import rpy2.robjects as robjects

        r = robjects.r
        # Create the data by passing a Python list to RPy2, which interprets as an R vector
        ctl = robjects.FloatVector([4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14])
        trt = robjects.FloatVector([4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69])
        group = r.gl(2, 10, 20, labels = ["Ctl","Trt"])
        weight = ctl + trt
        # RPy2 uses Python dictionary types to index data
        robjects.globalEnv["weight"] = weight
        robjects.globalEnv["group"] = group
        # Run the models
        lm_D9 = r.lm("weight ~ group")
        print(r.anova(lm_D9))

        lm_D90 = r.lm("weight ~ group - 1")
        print(r.summary(lm_D90))




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Calling lm from Python with RPy2


        Creating a linear model in Python
        # First import RPy2 and create an instance of robjects
        import rpy2.robjects as robjects

        r = robjects.r
        # Create the data by passing a Python list to RPy2, which interprets as an R vector
        ctl = robjects.FloatVector([4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14])
        trt = robjects.FloatVector([4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69])
        group = r.gl(2, 10, 20, labels = ["Ctl","Trt"])
        weight = ctl + trt
        # RPy2 uses Python dictionary types to index data
        robjects.globalEnv["weight"] = weight
        robjects.globalEnv["group"] = group
        # Run the models
        lm_D9 = r.lm("weight ~ group")
        print(r.anova(lm_D9))

        lm_D90 = r.lm("weight ~ group - 1")
        print(r.summary(lm_D90))



        For more info check out the RPy2 documentation (from where this example
        was stolen!)

                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Python Resources

        Where to get Python
               Binaries for several operating systems and chipsets can be downloaded at
               http://www.python.org/download/
               For OS X and Linux some version of Python comes pre-installed
               Some controversy over what version to use
        How to use Python
               Several very good development environments,
               once again SO has this covered
               My opinion, OS X: TextMate and Windows: Eclipse with PyDev, Linux:
               n/a
        How to Learn Python
               Several online resources: Dive into Python and Python Rocks
               For scientific computing: Enthought Webinars
               For applications to finance: NYC Financial Python User Group




                                                   Drew Conway        Learning R via Python...or the other way around
Introduction   Review of Python   Similar Data Structures   Example - Testing a Prime     Bridging the Gap with RPy2    Python Resources


Python Resources

        Where to get Python
               Binaries for several operating systems and chipsets can be downloaded at
               http://www.python.org/download/
               For OS X and Linux some version of Python comes pre-installed
               Some controversy over what version to use
        How to use Python
               Several very good development environments,
               once again SO has this covered
               My opinion, OS X: TextMate and Windows: Eclipse with PyDev, Linux:
               n/a
        How to Learn Python
               Several online resources: Dive into Python and Python Rocks
               For scientific computing: Enthought Webinars
               For applications to finance: NYC Financial Python User Group
        The best way to learn any language is to have a problem and solve it using that
        language!

                                                   Drew Conway        Learning R via Python...or the other way around

More Related Content

What's hot

Csr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminskiCsr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminski
CSR2011
 
Pré Descobrimento Do Brasil
Pré Descobrimento Do BrasilPré Descobrimento Do Brasil
Pré Descobrimento Do Brasil
ecsette
 
RProtoBuf: protocol buffers for R
RProtoBuf: protocol buffers for RRProtoBuf: protocol buffers for R
RProtoBuf: protocol buffers for R
Romain Francois
 

What's hot (20)

Mit203 analysis and design of algorithms
Mit203  analysis and design of algorithmsMit203  analysis and design of algorithms
Mit203 analysis and design of algorithms
 
IJCTT-V4I9P137
IJCTT-V4I9P137IJCTT-V4I9P137
IJCTT-V4I9P137
 
Python libraries
Python librariesPython libraries
Python libraries
 
Language models
Language modelsLanguage models
Language models
 
4.languagebasics
4.languagebasics4.languagebasics
4.languagebasics
 
An Efficient Search Engine for Searching Desired File
An Efficient Search Engine for Searching Desired FileAn Efficient Search Engine for Searching Desired File
An Efficient Search Engine for Searching Desired File
 
LaTeX 3 Paper
LaTeX 3 PaperLaTeX 3 Paper
LaTeX 3 Paper
 
P4 P Update January 2009
P4 P Update January 2009P4 P Update January 2009
P4 P Update January 2009
 
Ebay News 2001 4 19 Earnings
Ebay News 2001 4 19 EarningsEbay News 2001 4 19 Earnings
Ebay News 2001 4 19 Earnings
 
Ebay News 2000 10 19 Earnings
Ebay News 2000 10 19 EarningsEbay News 2000 10 19 Earnings
Ebay News 2000 10 19 Earnings
 
Zero to Hero - Introduction to Python3
Zero to Hero - Introduction to Python3Zero to Hero - Introduction to Python3
Zero to Hero - Introduction to Python3
 
Parallel Materialisation of Datalog Programs in Centralised, Main-Memory RDF ...
Parallel Materialisation of Datalog Programs in Centralised, Main-Memory RDF ...Parallel Materialisation of Datalog Programs in Centralised, Main-Memory RDF ...
Parallel Materialisation of Datalog Programs in Centralised, Main-Memory RDF ...
 
Csr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminskiCsr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminski
 
2P-Kt: logic programming with objects & functions in Kotlin
2P-Kt: logic programming with objects & functions in Kotlin2P-Kt: logic programming with objects & functions in Kotlin
2P-Kt: logic programming with objects & functions in Kotlin
 
Pré Descobrimento Do Brasil
Pré Descobrimento Do BrasilPré Descobrimento Do Brasil
Pré Descobrimento Do Brasil
 
A First Analysis of String APIs: the Case of Pharo
A First Analysis of String APIs: the Case of PharoA First Analysis of String APIs: the Case of Pharo
A First Analysis of String APIs: the Case of Pharo
 
Analysis of Similarity Measures between Short Text for the NTCIR-12 Short Tex...
Analysis of Similarity Measures between Short Text for the NTCIR-12 Short Tex...Analysis of Similarity Measures between Short Text for the NTCIR-12 Short Tex...
Analysis of Similarity Measures between Short Text for the NTCIR-12 Short Tex...
 
Ai unit-4
Ai unit-4Ai unit-4
Ai unit-4
 
RProtoBuf: protocol buffers for R
RProtoBuf: protocol buffers for RRProtoBuf: protocol buffers for R
RProtoBuf: protocol buffers for R
 
Algorithms and Their Explanations
Algorithms and Their ExplanationsAlgorithms and Their Explanations
Algorithms and Their Explanations
 

Similar to Learning R via Python…or the other way around

Accessing r from python using r py2
Accessing r from python using r py2Accessing r from python using r py2
Accessing r from python using r py2
Wisdio
 
Introduction to r bddsil meetup
Introduction to r bddsil meetupIntroduction to r bddsil meetup
Introduction to r bddsil meetup
Nimrod Priell
 

Similar to Learning R via Python…or the other way around (20)

Accessing r from python using r py2
Accessing r from python using r py2Accessing r from python using r py2
Accessing r from python using r py2
 
Special topics in finance lecture 2
Special topics in finance   lecture 2Special topics in finance   lecture 2
Special topics in finance lecture 2
 
Accessing R from Python using RPy2
Accessing R from Python using RPy2Accessing R from Python using RPy2
Accessing R from Python using RPy2
 
NumPy and SciPy for Data Mining and Data Analysis Including iPython, SciKits,...
NumPy and SciPy for Data Mining and Data Analysis Including iPython, SciKits,...NumPy and SciPy for Data Mining and Data Analysis Including iPython, SciKits,...
NumPy and SciPy for Data Mining and Data Analysis Including iPython, SciKits,...
 
1_Introduction.pptx
1_Introduction.pptx1_Introduction.pptx
1_Introduction.pptx
 
The Rise of Dynamic Languages
The Rise of Dynamic LanguagesThe Rise of Dynamic Languages
The Rise of Dynamic Languages
 
Python Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & stylePython Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & style
 
Intellectual technologies
Intellectual technologiesIntellectual technologies
Intellectual technologies
 
R Programming Language
R Programming LanguageR Programming Language
R Programming Language
 
R language
R languageR language
R language
 
Introduction to r bddsil meetup
Introduction to r bddsil meetupIntroduction to r bddsil meetup
Introduction to r bddsil meetup
 
summer training report on python
summer training report on pythonsummer training report on python
summer training report on python
 
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
 
Top Most Python Interview Questions.pdf
Top Most Python Interview Questions.pdfTop Most Python Interview Questions.pdf
Top Most Python Interview Questions.pdf
 
CPPDS Slide.pdf
CPPDS Slide.pdfCPPDS Slide.pdf
CPPDS Slide.pdf
 
Introduction_to_Python.pptx
Introduction_to_Python.pptxIntroduction_to_Python.pptx
Introduction_to_Python.pptx
 
R vs python
R vs pythonR vs python
R vs python
 
Presentation
PresentationPresentation
Presentation
 
Prolog & lisp
Prolog & lispProlog & lisp
Prolog & lisp
 
Python and r in data science
Python and r in data sciencePython and r in data science
Python and r in data science
 

More from Sid Xing (7)

Advanced dimensional modelling
Advanced dimensional modellingAdvanced dimensional modelling
Advanced dimensional modelling
 
Informatica PowerCenter 8.6.1 for windows installation
Informatica PowerCenter 8.6.1 for windows installationInformatica PowerCenter 8.6.1 for windows installation
Informatica PowerCenter 8.6.1 for windows installation
 
Oracle biee 使用administration创建仓库文件
Oracle biee 使用administration创建仓库文件Oracle biee 使用administration创建仓库文件
Oracle biee 使用administration创建仓库文件
 
Oracle oracle database 11g product family
Oracle oracle database 11g product familyOracle oracle database 11g product family
Oracle oracle database 11g product family
 
Oracle sql & plsql
Oracle sql & plsqlOracle sql & plsql
Oracle sql & plsql
 
Multidimensional analysis
Multidimensional analysisMultidimensional analysis
Multidimensional analysis
 
ERwin overview introduction
ERwin overview introductionERwin overview introduction
ERwin overview introduction
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 

Learning R via Python…or the other way around

  • 1. Learning R via Python ...or the other way around Drew Conway Dept. of Politics - NYU January 7, 2010
  • 2. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources What We’ll Cover Brief review of Python The Zen of Python How are R and Python the same, and how are they different Similar Data Structures Python dictionary R list Example - Testing if an integer is prime Create a function and returning a value Using while-loops Bridging the gap with RPy2 Running a regression with R inside Python Python resources Drew Conway Learning R via Python...or the other way around
  • 3. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Fundamental Elements of Python The Python coder’s creed >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren’t special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you’re Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it’s a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let’s do more of those! Drew Conway Learning R via Python...or the other way around
  • 4. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources How are R and Python alike, and how are they different? Similarities Differences Drew Conway Learning R via Python...or the other way around
  • 5. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources How are R and Python alike, and how are they different? Similarities Differences Functional programming paradigm Array of squared values # In Python we use a lambda nested in map >>> map(lambda x: x**2,range(1,11)) [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] Drew Conway Learning R via Python...or the other way around
  • 6. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources How are R and Python alike, and how are they different? Similarities Differences Functional programming paradigm Array of squared values # In Python we use a lambda nested in map >>> map(lambda x: x**2,range(1,11)) [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] # In R we define a function in sapply > sapply(1:10,function(x){return(x**2)}) [1] 1 4 9 16 25 36 49 64 81 100 Drew Conway Learning R via Python...or the other way around
  • 7. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources How are R and Python alike, and how are they different? Similarities Differences Functional programming paradigm Array of squared values # In Python we use a lambda nested in map >>> map(lambda x: x**2,range(1,11)) [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] # In R we define a function in sapply > sapply(1:10,function(x){return(x**2)}) [1] 1 4 9 16 25 36 49 64 81 100 Object-oriented programming Both languages support robust class structures In R, there are the S3 and S4 classes Drew Conway Learning R via Python...or the other way around
  • 8. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources How are R and Python alike, and how are they different? Similarities Differences Functional programming paradigm Array of squared values # In Python we use a lambda nested in map >>> map(lambda x: x**2,range(1,11)) [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] # In R we define a function in sapply > sapply(1:10,function(x){return(x**2)}) [1] 1 4 9 16 25 36 49 64 81 100 Object-oriented programming Both languages support robust class structures In R, there are the S3 and S4 classes Easily call C/Fortran code Due to their high-level, both languages provide functionality to call compiled code from lower-level languages Drew Conway Learning R via Python...or the other way around
  • 9. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources How are R and Python alike, and how are they different? Similarities Differences Functional programming paradigm General purpose OOP vs. Functional OOP Python is a high-level language for Array of squared values application development # In Python we use a lambda nested in map >>> map(lambda x: x**2,range(1,11)) R is a collection of functions for data [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] analysis # In R we define a function in sapply > sapply(1:10,function(x){return(x**2)}) [1] 1 4 9 16 25 36 49 64 81 100 Object-oriented programming Both languages support robust class structures In R, there are the S3 and S4 classes Easily call C/Fortran code Due to their high-level, both languages provide functionality to call compiled code from lower-level languages Drew Conway Learning R via Python...or the other way around
  • 10. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources How are R and Python alike, and how are they different? Similarities Differences Functional programming paradigm General purpose OOP vs. Functional OOP Python is a high-level language for Array of squared values application development # In Python we use a lambda nested in map >>> map(lambda x: x**2,range(1,11)) R is a collection of functions for data [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] analysis # In R we define a function in sapply Syntax structure > sapply(1:10,function(x){return(x**2)}) [1] 1 4 9 16 25 36 49 64 81 100 Python syntax forces readability through whitespace R emphasizes parsimony and nesting Object-oriented programming Both languages support robust class structures In R, there are the S3 and S4 classes Easily call C/Fortran code Due to their high-level, both languages provide functionality to call compiled code from lower-level languages Drew Conway Learning R via Python...or the other way around
  • 11. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources How are R and Python alike, and how are they different? Similarities Differences Functional programming paradigm General purpose OOP vs. Functional OOP Python is a high-level language for Array of squared values application development # In Python we use a lambda nested in map >>> map(lambda x: x**2,range(1,11)) R is a collection of functions for data [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] analysis # In R we define a function in sapply Syntax structure > sapply(1:10,function(x){return(x**2)}) [1] 1 4 9 16 25 36 49 64 81 100 Python syntax forces readability through whitespace R emphasizes parsimony and nesting Object-oriented programming Both languages support robust class Creating a function structures In R, there are the S3 and S4 classes # In Python functions are declared >>> def my func(x): Easily call C/Fortran code ... Due to their high-level, both languages provide functionality to call compiled code from lower-level languages Drew Conway Learning R via Python...or the other way around
  • 12. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources How are R and Python alike, and how are they different? Similarities Differences Functional programming paradigm General purpose OOP vs. Functional OOP Python is a high-level language for Array of squared values application development # In Python we use a lambda nested in map >>> map(lambda x: x**2,range(1,11)) R is a collection of functions for data [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] analysis # In R we define a function in sapply Syntax structure > sapply(1:10,function(x){return(x**2)}) [1] 1 4 9 16 25 36 49 64 81 100 Python syntax forces readability through whitespace R emphasizes parsimony and nesting Object-oriented programming Both languages support robust class Creating a function structures In R, there are the S3 and S4 classes # In Python functions are declared >>> def my func(x): Easily call C/Fortran code ... Due to their high-level, both languages # In R functions are assigned > my.func<-function(x) { ... } provide functionality to call compiled code from lower-level languages Drew Conway Learning R via Python...or the other way around
  • 13. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources How are R and Python alike, and how are they different? Similarities Differences Functional programming paradigm General purpose OOP vs. Functional OOP Python is a high-level language for Array of squared values application development # In Python we use a lambda nested in map >>> map(lambda x: x**2,range(1,11)) R is a collection of functions for data [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] analysis # In R we define a function in sapply Syntax structure > sapply(1:10,function(x){return(x**2)}) [1] 1 4 9 16 25 36 49 64 81 100 Python syntax forces readability through whitespace R emphasizes parsimony and nesting Object-oriented programming Both languages support robust class Creating a function structures In R, there are the S3 and S4 classes # In Python functions are declared >>> def my func(x): Easily call C/Fortran code ... Due to their high-level, both languages # In R functions are assigned > my.func<-function(x) { ... } provide functionality to call compiled code from lower-level languages For more info see: StackOverlow.com discussion on topic Drew Conway Learning R via Python...or the other way around
  • 14. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Python Dictionaries In Python the dict type is a data structure that represents a key→value mapping Drew Conway Learning R via Python...or the other way around
  • 15. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Python Dictionaries In Python the dict type is a data structure that represents a key→value mapping Working with the dict type # Keys and values can be of any data type >>> fruit dict={"apple":1,"orange":[0.23,0.11],"banana":True } Drew Conway Learning R via Python...or the other way around
  • 16. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Python Dictionaries In Python the dict type is a data structure that represents a key→value mapping Working with the dict type # Keys and values can be of any data type >>> fruit dict={"apple":1,"orange":[0.23,0.11],"banana":True } # Can retrieve the keys and values as Python lists (vector) >>> fruit dict.keys() ["orange","apple","banana"] Drew Conway Learning R via Python...or the other way around
  • 17. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Python Dictionaries In Python the dict type is a data structure that represents a key→value mapping Working with the dict type # Keys and values can be of any data type >>> fruit dict={"apple":1,"orange":[0.23,0.11],"banana":True } # Can retrieve the keys and values as Python lists (vector) >>> fruit dict.keys() ["orange","apple","banana"] # Or create a (key,value) tuple >>> fruit dict.items() [("orange",[0.23,0.11]),("apple",1),("Banana",True)] Drew Conway Learning R via Python...or the other way around
  • 18. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Python Dictionaries In Python the dict type is a data structure that represents a key→value mapping Working with the dict type # Keys and values can be of any data type >>> fruit dict={"apple":1,"orange":[0.23,0.11],"banana":True } # Can retrieve the keys and values as Python lists (vector) >>> fruit dict.keys() ["orange","apple","banana"] # Or create a (key,value) tuple >>> fruit dict.items() [("orange",[0.23,0.11]),("apple",1),("Banana",True)] # This becomes especially useful when you master Python ‘‘list comprehension’’ Drew Conway Learning R via Python...or the other way around
  • 19. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Python Dictionaries In Python the dict type is a data structure that represents a key→value mapping Working with the dict type # Keys and values can be of any data type >>> fruit dict={"apple":1,"orange":[0.23,0.11],"banana":True } # Can retrieve the keys and values as Python lists (vector) >>> fruit dict.keys() ["orange","apple","banana"] # Or create a (key,value) tuple >>> fruit dict.items() [("orange",[0.23,0.11]),("apple",1),("Banana",True)] # This becomes especially useful when you master Python ‘‘list comprehension’’ The Python dictionary is an extremely flexible and useful data structure, making it one of the primary advantages of Python over other languages Luckily, there is a fairly direct mapping between the Python dict and R lists! Drew Conway Learning R via Python...or the other way around
  • 20. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources R Lists Similarly to the Python dictionary, the R list is a key→value mapping Drew Conway Learning R via Python...or the other way around
  • 21. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources R Lists Similarly to the Python dictionary, the R list is a key→value mapping Working with an R list > fruit.list<-list("apple"=1,"orange"=c(0.23,0.11),"banana"=TRUE) > fruit.list $apple [1] 1 $orange [1] 0.55 0.11 $banana [1] TRUE Drew Conway Learning R via Python...or the other way around
  • 22. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources R Lists Similarly to the Python dictionary, the R list is a key→value mapping Working with an R list > fruit.list<-list("apple"=1,"orange"=c(0.23,0.11),"banana"=TRUE) > fruit.list $apple [1] 1 $orange [1] 0.55 0.11 $banana [1] TRUE Notice, however, that R will always treat the value of the key/value pair as a vector; unlike Python, which does not care the value’s data type. Of the many R programming paradigms, the “vectorization of all data” is among the strongest. Drew Conway Learning R via Python...or the other way around
  • 23. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources R Lists Similarly to the Python dictionary, the R list is a key→value mapping Working with an R list > fruit.list<-list("apple"=1,"orange"=c(0.23,0.11),"banana"=TRUE) > fruit.list $apple [1] 1 $orange [1] 0.55 0.11 $banana [1] TRUE Notice, however, that R will always treat the value of the key/value pair as a vector; unlike Python, which does not care the value’s data type. Of the many R programming paradigms, the “vectorization of all data” is among the strongest. Using unlist > unlist(fruist.list) apple orange1 orange2 banana 1.00 0.55 0.11 1.00 Drew Conway Learning R via Python...or the other way around
  • 24. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Testing a Prime in Python We are interested in testing whether integer X is prime, and to do so we will: Declare a function Use a while-loop Return a boolean Drew Conway Learning R via Python...or the other way around
  • 25. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Testing a Prime in Python We are interested in testing whether integer X is prime, and to do so we will: Declare a function Use a while-loop Return a boolean The is prime function def is prime(x): if x%2 == 0: return False else: y=3 while y<x: if x%y==0: return False else: y+=2 return True Drew Conway Learning R via Python...or the other way around
  • 26. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Testing a Prime in Python We are interested in testing whether integer X is prime, and to do so we will: Declare a function Use a while-loop Return a boolean The is prime function def is prime(x): if x%2 == 0: return False else: y=3 while y<x: if x%y==0: return False else: y+=2 return True Notice the number of lines in this simple function. In contrast to R, and many other programming languages, Python’s whitespace and indentation requirements force verbose code; however, the end result is very readable. Drew Conway Learning R via Python...or the other way around
  • 27. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Testing a Prime in R Unlike Python where functions are declared explicitly, R creates functions through assignment. In many ways, this is somewhere between the Python lambda function and an explicit function declaration. Drew Conway Learning R via Python...or the other way around
  • 28. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Testing a Prime in R Unlike Python where functions are declared explicitly, R creates functions through assignment. In many ways, this is somewhere between the Python lambda function and an explicit function declaration. The is.prime function is.prime<-function(x) { if(x%%2==0) {return(FALSE) } else { y<-3 while(y<x) ifelse(x%%y==0,return(FALSE),y<-y+2) } return(TRUE) } Drew Conway Learning R via Python...or the other way around
  • 29. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Testing a Prime in R Unlike Python where functions are declared explicitly, R creates functions through assignment. In many ways, this is somewhere between the Python lambda function and an explicit function declaration. The is.prime function is.prime<-function(x) { if(x%%2==0) {return(FALSE) } else { y<-3 while(y<x) ifelse(x%%y==0,return(FALSE),y<-y+2) } return(TRUE) } Contrast the width of this version of the function with the length of the previous With the ifelse function R allows you to compress many lines of code A good example of the difference between a largely function programming language with objects (R) vs. a largely objected-oriented language with a core set of functions (Python) Drew Conway Learning R via Python...or the other way around
  • 30. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Bridging the Gap with RPy2 Python is widely adopted within the scientific and data communities The combination of SciPy/NumPy/matplotlib represents a powerful set of tools for scientific computing Companies like Enthought are focused on servicing this area Drew Conway Learning R via Python...or the other way around
  • 31. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Bridging the Gap with RPy2 Python is widely adopted within the scientific and data communities The combination of SciPy/NumPy/matplotlib represents a powerful set of tools for scientific computing Companies like Enthought are focused on servicing this area There are, however, many times when even this combination cannot match the analytical power of R—enter RPy2 RPy2 is a Python package that allows you to call R directly from within a Python script This is particularly useful if you want to use one of R’s base functions inside Python Drew Conway Learning R via Python...or the other way around
  • 32. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Bridging the Gap with RPy2 Python is widely adopted within the scientific and data communities The combination of SciPy/NumPy/matplotlib represents a powerful set of tools for scientific computing Companies like Enthought are focused on servicing this area There are, however, many times when even this combination cannot match the analytical power of R—enter RPy2 RPy2 is a Python package that allows you to call R directly from within a Python script This is particularly useful if you want to use one of R’s base functions inside Python For example, creating a function to mimic R’s lm in Python—even using the above combination—would be very time consuming. To avoid this, we will use RPy2 to call lm from within a Python script and plot the data. Drew Conway Learning R via Python...or the other way around
  • 33. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Bridging the Gap with RPy2 Python is widely adopted within the scientific and data communities The combination of SciPy/NumPy/matplotlib represents a powerful set of tools for scientific computing Companies like Enthought are focused on servicing this area There are, however, many times when even this combination cannot match the analytical power of R—enter RPy2 RPy2 is a Python package that allows you to call R directly from within a Python script This is particularly useful if you want to use one of R’s base functions inside Python For example, creating a function to mimic R’s lm in Python—even using the above combination—would be very time consuming. To avoid this, we will use RPy2 to call lm from within a Python script and plot the data. Disclaimer: the syntax can be a bit tricky to understand at first, so allow for a fairly steep learning curve! Drew Conway Learning R via Python...or the other way around
  • 34. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Calling lm from Python with RPy2 Creating a linear model in Python Drew Conway Learning R via Python...or the other way around
  • 35. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Calling lm from Python with RPy2 Creating a linear model in Python # First import RPy2 and create an instance of robjects import rpy2.robjects as robjects r = robjects.r Drew Conway Learning R via Python...or the other way around
  • 36. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Calling lm from Python with RPy2 Creating a linear model in Python # First import RPy2 and create an instance of robjects import rpy2.robjects as robjects r = robjects.r # Create the data by passing a Python list to RPy2, which interprets as an R vector ctl = robjects.FloatVector([4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14]) trt = robjects.FloatVector([4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69]) group = r.gl(2, 10, 20, labels = ["Ctl","Trt"]) weight = ctl + trt Drew Conway Learning R via Python...or the other way around
  • 37. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Calling lm from Python with RPy2 Creating a linear model in Python # First import RPy2 and create an instance of robjects import rpy2.robjects as robjects r = robjects.r # Create the data by passing a Python list to RPy2, which interprets as an R vector ctl = robjects.FloatVector([4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14]) trt = robjects.FloatVector([4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69]) group = r.gl(2, 10, 20, labels = ["Ctl","Trt"]) weight = ctl + trt # RPy2 uses Python dictionary types to index data robjects.globalEnv["weight"] = weight robjects.globalEnv["group"] = group Drew Conway Learning R via Python...or the other way around
  • 38. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Calling lm from Python with RPy2 Creating a linear model in Python # First import RPy2 and create an instance of robjects import rpy2.robjects as robjects r = robjects.r # Create the data by passing a Python list to RPy2, which interprets as an R vector ctl = robjects.FloatVector([4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14]) trt = robjects.FloatVector([4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69]) group = r.gl(2, 10, 20, labels = ["Ctl","Trt"]) weight = ctl + trt # RPy2 uses Python dictionary types to index data robjects.globalEnv["weight"] = weight robjects.globalEnv["group"] = group # Run the models lm_D9 = r.lm("weight ~ group") print(r.anova(lm_D9)) lm_D90 = r.lm("weight ~ group - 1") print(r.summary(lm_D90)) Drew Conway Learning R via Python...or the other way around
  • 39. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Calling lm from Python with RPy2 Creating a linear model in Python # First import RPy2 and create an instance of robjects import rpy2.robjects as robjects r = robjects.r # Create the data by passing a Python list to RPy2, which interprets as an R vector ctl = robjects.FloatVector([4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14]) trt = robjects.FloatVector([4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69]) group = r.gl(2, 10, 20, labels = ["Ctl","Trt"]) weight = ctl + trt # RPy2 uses Python dictionary types to index data robjects.globalEnv["weight"] = weight robjects.globalEnv["group"] = group # Run the models lm_D9 = r.lm("weight ~ group") print(r.anova(lm_D9)) lm_D90 = r.lm("weight ~ group - 1") print(r.summary(lm_D90)) For more info check out the RPy2 documentation (from where this example was stolen!) Drew Conway Learning R via Python...or the other way around
  • 40. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Python Resources Where to get Python Binaries for several operating systems and chipsets can be downloaded at http://www.python.org/download/ For OS X and Linux some version of Python comes pre-installed Some controversy over what version to use How to use Python Several very good development environments, once again SO has this covered My opinion, OS X: TextMate and Windows: Eclipse with PyDev, Linux: n/a How to Learn Python Several online resources: Dive into Python and Python Rocks For scientific computing: Enthought Webinars For applications to finance: NYC Financial Python User Group Drew Conway Learning R via Python...or the other way around
  • 41. Introduction Review of Python Similar Data Structures Example - Testing a Prime Bridging the Gap with RPy2 Python Resources Python Resources Where to get Python Binaries for several operating systems and chipsets can be downloaded at http://www.python.org/download/ For OS X and Linux some version of Python comes pre-installed Some controversy over what version to use How to use Python Several very good development environments, once again SO has this covered My opinion, OS X: TextMate and Windows: Eclipse with PyDev, Linux: n/a How to Learn Python Several online resources: Dive into Python and Python Rocks For scientific computing: Enthought Webinars For applications to finance: NYC Financial Python User Group The best way to learn any language is to have a problem and solve it using that language! Drew Conway Learning R via Python...or the other way around