Set Up

library(reticulate) # to use python in RStudio
library(tidyverse) # data wrangling and plotting with R

R code chunks are in light pink, while python in light blue.

Highlight

  • In Python, methods are a specific type of functions that are inherited by a particular type of object or class .
  • To find out what methods are available for an object, we can
    • use dir()
    • add a dot after the object then press Tab for a list of usable methods

Functions

We have functions in both R and Python. The way to call them is sort of like how you would call an excel function. Please see examples below.

x <- -3.58

abs(x)
## [1] 3.58
round(x)
## [1] -4
x = -3.5

abs(x)
## 3.5
round(x)
## -4

Methods

In python, methods are a specific type of functions that are inherited by a particular type of object or class .

Just now we created an object “x” and assigned the value “-3.5” to it. It is a float type of variable. Let’s explore what methods are available to this object using the function dir().

type(x)
## <class 'float'>
dir(x)
## ['__abs__', '__add__', '__bool__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__int__', '__le__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__pos__', '__pow__', '__radd__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__round__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__setformat__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real']

Let’s try some methods. Notice when we use a method, we put a dot after the object and then type out the method. You could also type the object “x” and the dot first then press Tab, a list of usable methods will appear for you to choose from. :)

x.as_integer_ratio()
## (-7, 2)
x.is_integer()
## False

Let’s try a string type variable in python and see what methods are available for it.

y = "I love chocolate cookies"

type(y)
## <class 'str'>
dir(y)
## ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
y.lower()
## 'i love chocolate cookies'
y # notice here applying this method to y didn't change y itself
## 'I love chocolate cookies'
y.title()
## 'I Love Chocolate Cookies'
y.capitalize()
## 'I love chocolate cookies'

Functional programming vs OOP

As an R user, I really enjoy how we can perform series of tasks on a dataset using a pipeline of functions (pls see follows a random example). But I do look forward to learning more about OOP (Object Oriented Programming) and the power and convenience it brings. :)

mpg |> 
  # get some summary statistics for each model
  group_by(model) |> 
  summarize(hwy_mean = mean(hwy),
            displ_median = median(displ)) |> 
  ungroup() |> 
  
  # filter the models by the value of summary statistics
  filter(hwy_mean > 16,
         displ_median > 5)  |> 
  
  # make a plot based on the result
  ggplot(aes(y = reorder(model,hwy_mean),x = hwy_mean))+
  geom_col(fill = "turquoise", alpha = 0.6) +
  
  # set the appearance of the plot
  theme_minimal() +
  labs(title = "Models with mean_hwy > 16 and median_displ > 5",y="",x="Mean HWY")