Introduction to R

Week 6, Part 3

Jeff Jacobs

jj1088@georgetown.edu
DSAN Summer Bootcamp 2024
Georgetown University

R for Pythoners

The Most Immediately-Jarring Differences

Python
R

Value Assignment

<varname> = <value>

x = 3
x
3

<varname> <- <value>

x <- 3
x
[1] 3

Indices

Start from 0

my_list = ["a", "b", "c"]
my_list[0] # Accesses first element
'a'
my_list[1] # Accesses second element
'b'

Start from 1

my_vector <- c("a", "b", "c")
my_vector[0] # Invalid index in R!
character(0)
my_vector[1] # Accesses first element
[1] "a"
my_vector[2] # Accesses second element
[1] "b"

Booleans

True, False

result = 2 * 2 == 4
result
True

TRUE (or T), FALSE (or F)

result <- 2 * 2 == 4
result
[1] TRUE

Everything is a Vector

  • Although there are R equivalents for most Python data types, one big difference is that…
  • In R, everything is a vector!

Numbers

x <- 3
print(x)
[1] 3
class(x)
[1] "numeric"
length(x)
[1] 1

Length-1 Vectors

y <- c(3)
print(y)
[1] 3
class(y)
[1] "numeric"
length(y)
[1] 1

Length-N Vectors

z <- c(3, 4, 5)
print(z)
[1] 3 4 5
class(z)
[1] "numeric"
length(z)
[1] 3

Careful With “Basic” Operators Like +!

  • If you’re used to Python, + feels like a natural way to combine things

  • R forces you to think explicitly about combining (\(\neq\) adding): more restrictive +

  • For strings:

    In Python: + means concatenate

    message = "Hi" + " " + "Jeff"
    print(message)
    Hi Jeff

    In R: + is not allowed!

    my_message <- "Hi" + " " + "Jeff"
    Error in "Hi" + " ": non-numeric argument to binary operator
  • For numeric lists…

    In Python: + means concatenate

    [1, 2] + [3, 4]
    [1, 2, 3, 4]

    In R: + means vector addition!

    c(1,2) + c(3,4)
    [1] 4 6

Immensely Important Distinction

  • Throughout bootcamp, throughout DSAN, throughout your career, you’ll use two different “flavors” of R:

Base R

base_df <- data.frame(
  x=c(1,2,3),
  y=c("a","b","c")
)
base_df
x y
1 a
2 b
3 c
class(base_df)
[1] "data.frame"

Tidyverse

library(tidyverse) # Imports "tibble" library
tv_df <- tribble(
  ~x, ~y,
  1, "a",
  2, "b",
  3, "c"
)
tv_df
x y
1 a
2 b
3 c
class(tv_df)
[1] "tbl_df"     "tbl"        "data.frame"

Why Are There Two Ways to Do Everything?

  • One key reason: tidyverse built on the pipe operator |>

  • Recognition that data science often involves pipelines: chains of operations performed in sequence

    • E.g., scrape data → clean data → save to DB
  • Base R approach:

    result <- saveToDB(cleanData(scrapeData()))
  • Tidyverse approach:

    result <- scrapeData() |> cleanData() |> saveToDB()
  • Now imagine pipelines used by giant companies, e.g., training Netflix’s recommendation engine!

|>: Good News and Bad News

  • f() |> g() just means: take the result of the call f() and pass it as the first argument to g()
  • Good news: this means |> is intuitively “backwards compatible” with many base R functions
  • Bad news: requires checking the first argument:
getSampleSize <- function() {
  return(20)
}
getSampleSize() |> rbinom(10, 0.5)
 [1] 3 5 7 3 4 5 5 8 3 5 5 2 6 5 2 2 5 6 6 4
getNumTrials <- function() {
  return(10)
}
getNumTrials() |> rbinom(100, 0.5)
 [1] 51 47 50 55 45 43 46 58 60 53

[Quick Note:]

  • If you’ve used R before, you may have seen the %>% operator from tidyverse
  • …It got so popular that it is now built into R (as of 2023) as |>!
  • \(\implies\) Switch from %>% to |> for instant efficiency boost 😎

Back to Basics

Combining Strings

  • Earlier we saw how R doesn’t allow "Hi" + " " + "Jeff"…

  • So how do we combine strings?

  • paste() and paste0()!

    paste("Hi", "Jeff")
    [1] "Hi Jeff"
    paste0("Hi", " ", "Jeff")
    [1] "Hi Jeff"

Python Lists \(\leftrightarrow\) R Vectors

Creation
my_list = [1, 2, 3]
my_list
[1, 2, 3]
my_vector <- c(1, 2, 3)
my_vector
[1] 1 2 3
Insertion
my_list.append(4)
my_list
[1, 2, 3, 4]
my_vector <- c(my_vector, 4)
my_vector
[1] 1 2 3 4
Replicating Elements
my_list * 3
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
rep(my_vector, 3)
 [1] 1 2 3 4 1 2 3 4 1 2 3 4
Python range() \(\leftrightarrow\) R seq()
list(range(5, 10))
[5, 6, 7, 8, 9]
seq(5, 10)
[1]  5  6  7  8  9 10

paste0() with Vectors!

names <- c("Aaliya", "Brandon", "Cyrus")
paste0(names, collapse=" ")
[1] "Aaliya Brandon Cyrus"
paste0(names, collapse=", ")
[1] "Aaliya, Brandon, Cyrus"
paste(names, collapse=", ")
[1] "Aaliya, Brandon, Cyrus"

Semi-Confusing Point: R Also Has Lists…

  • Rarely used, despite greater similarity to Python lists 😵‍💫

  • Key difference: all elements of a vector must have same type; elements of a list may have different types

    my_list <- list("A", 2, TRUE)
    my_list
    [[1]]
    [1] "A"
    
    [[2]]
    [1] 2
    
    [[3]]
    [1] TRUE
  • (Note how each element is a vector: character vector, integer vector, and boolean vector)

DataFrames: Tabular Data Formats

Col A Col B
Row 1 Val 1A Val 1B
Row 2 Val 2A Val 2B
Figure 1: A (tidy) tabular dataset, with row indices in blue, column names in orange, and values in green

Pandas DataFrame \(\leftrightarrow\) R data.frame

import pandas as pd
x_data = [1, 2, 3]
y_data = [4, 5, 6]
df = pd.DataFrame({
  'x': x_data, 'y': y_data
})
print(df.to_markdown())
x y
0 1 4
1 2 5
2 3 6
x_data <- c(1, 2, 3)
y_data <- c(4, 5, 6)
df <- data.frame(
  x=x_data,
  y=y_data
)
df
x y
1 4
2 5
3 6

The Inevitable Headaches of Missing Data in Python

Given a .csv with missing entries:

my_data.csv
id,name,email
0,jeff,jeff@jeff.com
1,,nobody@no.com
3,zayd,z@email.biz

Python defaults to object columns with NaN (Not-a-Number) values

import pandas as pd
df = pd.read_csv("my_data.csv")
df
   id  name          email
0   0  jeff  jeff@jeff.com
1   1   NaN  nobody@no.com
2   3  zayd    z@email.biz
df.dtypes
id        int64
name     object
email    object
dtype: object

Intuitive fix usually isn’t what you want:

df_bad = df.copy()
df_bad['name'] = df_bad['name'].astype(str)
df_bad
   id  name          email
0   0  jeff  jeff@jeff.com
1   1   nan  nobody@no.com
2   3  zayd    z@email.biz
df_bad.iloc[1]['name']
'nan'

This alternative approach ensures missing values are empty strings

df_better = df.copy()
df_better['name'] = df_better['name'].fillna('').astype(str)
df_better
   id  name          email
0   0  jeff  jeff@jeff.com
1   1        nobody@no.com
2   3  zayd    z@email.biz
df_better.iloc[1]['name']
''

The Slightly-Less-Headachey R Approach!

R reads missing values in character columns as empty strings by default

df <- read.csv("my_data.csv")
df
id name email
0 jeff jeff@jeff.com
1 nobody@no.com
3 zayd z@email.biz
df$name
[1] "jeff" ""     "zayd"
# We'll learn about sapply() next week!
sapply(df, class)
         id        name       email 
  "integer" "character" "character" 

…And also has a built-in NA value!

my_data_2.csv
id,name,email,fav_num
0,jeff,jeff@jeff.com,3.14
1,,nobody@no.com,
3,zayd,z@email.biz,11
df2 <- read.csv("my_data_2.csv")
df2
id name email fav_num
0 jeff jeff@jeff.com 3.14
1 nobody@no.com NA
3 zayd z@email.biz 11.00
sapply(df2, class)
         id        name       email     fav_num 
  "integer" "character" "character"   "numeric" 

Bonus: Built-In Matrices

  • For data analysis (cleaning, plotting), data.frame is usually the way to go

  • For linear algebra (inverting, multiplying matrices)…

    Use 2D vectors!

    my_matrix <- matrix(
      data=c(1, 2, 3, 4),
      nrow=2, ncol=2, byrow=TRUE
    )
    my_matrix
         [,1] [,2]
    [1,]    1    2
    [2,]    3    4
    class(my_matrix)
    [1] "matrix" "array" 

    Beware! Matrix multiply operator is %*%, NOT *:

    my_matrix * my_matrix
         [,1] [,2]
    [1,]    1    4
    [2,]    9   16
    my_matrix %*% my_matrix
         [,1] [,2]
    [1,]    7   10
    [2,]   15   22

Filtering (Selecting Rows)

  • In Base R, filtering involves two steps:

  • Create a boolean expression that will evaluate to TRUE for the rows/columns you want to keep, and FALSE otherwise

  • Use it in the correct “slot” of R’s access operator []:

    filtered_df <- df[<row filter>, <column filter>] # Note: Either (or both) can be left blank!
  • R will go through each row, evaluate <row filter>, and keep the row if and only if <row filter> evaluates to TRUE

  • Example: Selecting only rows with odd-numbered id values:

    # Boolean expression which is TRUE for
    # odd-numbered ids
    my_expression <- df2$id %% 2 == 1
    my_expression
    [1] FALSE  TRUE  TRUE
    filtered_df <- df2[my_expression,]
    filtered_df
    id name email fav_num
    2 1 nobody@no.com NA
    3 3 zayd z@email.biz 11

Selecting Columns

  • Create a vector with names of columns you want to keep:

    cols_to_keep <- c("var1", "var2", "var3")
  • Use as the sole argument to R’s access operator []:

    subset_df <- df[cols_to_keep]
  • Example: Keeping just the name and fav_num columns:

    cols_to_keep <- c("name", "fav_num")
    subset_df <- filtered_df[cols_to_keep]
    subset_df
    name fav_num
    2 NA
    3 zayd 11

Sampling

  • Filtering = choosing rows deterministically
  • Sampling = choosing rows randomly
  • R has a built-in sample() function, but be careful! It does not work directly on data.frame objects 😱 Instead:

Sample row indices:

set.seed(2024)
indices <- sample(nrow(df), 2)
indices
[1] 2 1

Select only these indices:

sample_df <- df2[indices,]
sample_df
id name email fav_num
2 1 nobody@no.com NA
1 0 jeff jeff@jeff.com 3.14