Functions

STAT 220

Bastola

Functions

A function is a type of object in R that can perform a specific task.

  • Functions take arguments as input and output some manipulated form of the input data.
  • A function is specified first with the object name, then parentheses with arguments inside.
# using the sqrt function
sqrt(4)
[1] 2

When to write functions?

  • Using the same code more than once
  • Complicated operation
  • Vectorization

Function arguments

  • x, y, z: vectors.
  • w: a vector of weights.
  • df: a data frame or tibble
  • i, j: numeric indices (typically rows and columns).
  • n: length, or number of rows.
  • p: number of columns.

Writing Functions

# Basic Set Up
my_awesome_function <- function(x,y) # arg1, arg2, etc. 

Writing Functions

# Basic Set Up
my_awesome_function <- function(x,y) ## arg1, arg2, etc.
{ # Brackets that house the code 

Writing Functions

# Basic Set Up
my_awesome_function <- function(x,y) # arg1, arg2, etc.
{ # Brackets that house the code 
  # Some code to execute  

Writing Functions

# Basic Set Up
my_awesome_function <- function(x,y) # arg1, arg2, etc.
{ # Brackets that house the code 
  # Some code to execute 
  z = x*y  

Writing Functions

# Basic Set Up
my_awesome_function <- function(x,y) # arg1, arg2, etc.
{ # Brackets that house the code 
  # Some code to execute 
  z = x*y  
  return(z) # Return a data value 

Writing Functions

# Basic Set Up
my_awesome_function <- function(x,y) # arg1, arg2, etc.
{ # Brackets that house the code 
  # Some code to execute 
  z = x*y  
  return(z) # Return a data value  
} # Close the Brackets   

Writing Functions

# Basic Set Up
my_awesome_function <- function(x,y) # arg1, arg2, etc. 
{ # Brackets that house the code 
  # Some code to execute 
  z = x*y  
  return(z) # Return a data value  
} # Close the Brackets   


# Call the function
my_awesome_function(x=5,y=6) 
[1] 30

 Group Activity 1


  • Please clone the ca13-yourusername repository from Github
  • Please do the problem 1 in the class activity for today

10:00

Conditional Execution



Allows code to:

  • become more flexible
  • adapt to the input arguments
  • have certain “control flow” constructs

if-else

if(TRUE){
  print("Positive")
}else{
  print("Negative")
}
[1] "Positive"
if(FALSE){
  print("Positive")
}else{
  print("Negative")
}
[1] "Negative"

ifelse()

  • Same idea just vectorized
ifelse(TRUE,"Positive","Negative")
[1] "Positive"
dplyr::if_else(FALSE,"Positive","Negative")
[1] "Negative"
x <- 1:5
ifelse(x<3, "Positive", "Negative")
[1] "Positive" "Positive" "Negative" "Negative" "Negative"

if and ifelse

# Use `if` for single condition tests
cutoff_make0 <- function(x, cutoff = 0){   # default cutoff is 0
  if(is.numeric(x)){
    ifelse(x < cutoff, 0, x)
  } else warning("The input provided is not a numeric vector")
}

if and ifelse: Implementation

x <- c(3, 4, 6, -1)
y <- c("5", "c", "9", 1)
# override the default cutoff of 0
cutoff_make0(x, cutoff = 4)
[1] 0 4 6 0
# no cutoff given, defaults to 0
cutoff_make0(x)
[1] 3 4 6 0
cutoff_make0(y, cutoff = 4)
Warning in cutoff_make0(y, cutoff = 4): The input provided is not a numeric
vector

Tidy Text


  • tidy data principles
  • works with existing data manipulation tools
  • streamlined integration with other text mining libraries

Quote from Neil deGrasse Tyson

text <- c("It's not that we are better than the universe, we are part of the
universe. We are in the universe and the universe is in us. We are all connected; 
To each other, biologically.")
text_data <- tibble(text = text)
text_data %>% pull(text) %>%  writeLines()
It's not that we are better than the universe, we are part 
of the universe. We are in the universe and the universe is in us.
We are all connected; To each other, biologically.

Tokenization

text_data %>% 
  unnest_tokens(output = word, 
                input = text, 
                token = "words") 
# A tibble: 34 × 1
   word    
   <chr>   
 1 it's    
 2 not     
 3 that    
 4 we      
 5 are     
 6 better  
 7 than    
 8 the     
 9 universe
10 we      
# ℹ 24 more rows

Counting words

text_data %>% 
  unnest_tokens(word,text) %>% 
  count(word, sort = TRUE)
# A tibble: 21 × 2
   word             n
   <chr>        <int>
 1 are              4
 2 the              4
 3 universe         4
 4 we               4
 5 in               2
 6 all              1
 7 and              1
 8 better           1
 9 biologically     1
10 connected        1
# ℹ 11 more rows

Stopwords

  • tidytext comes with a database of common stop words
stop_words %>% sample_n(10)
# A tibble: 10 × 2
   word    lexicon 
   <chr>   <chr>   
 1 in      SMART   
 2 how     onix    
 3 old     SMART   
 4 doesn't snowball
 5 would   snowball
 6 here    snowball
 7 cannot  SMART   
 8 very    snowball
 9 seven   SMART   
10 any     onix    

Amazon musical instruments reviews

Musical Instruments
What is the average sentiment of Amazon 
shoppers purchasing musical instruments?

Glimpse of the dataset

reviewerName reviewText ratingOverall
StormJH1 i acknowledge that this is a minority opinion perhaps i just had bad luck with my unit and the attempts to repair it afterwards but while i loved the sound and functionality of this wah wah pedal it would literally last me a few months at a time before it became unusable i suspected that the potentiameter was shot so i had that replaced but the same problems reoccurred like a horrible static noise as soon as started using it good product but hopefully they make them more durable than the particular unit i had 3
J. Edgar Mihelic “Skyscraper” i had put these on and my wife picked my guitar to play and she was confused they do take some time getting used to they do not rip up your hand as much and if you keep your fingers on the strings as you shift there is less friction this is good but it takes some getting used to i used that finger tip friction to let me know where i was on the fretboard with these i found myself over shifting both high and low i like them and they sound nice but i bought some round wounds to replace these when the time comes 4
Nicholas A. Douglas “b4mvfan01” these pedals are great exclamationmark exclamationmark a friend of mine said i should buy other pedals because these pedals are not manufactured by a very known brand name but i went with my own judgement and i was not disappointed with them i have increased my speed a lot since I have gotten em and i can pedal songs like all nightmare long and beast and the harlot now equals great pedals but it takes time to develop speed so do not think the pedals suck just because you barely get em and you can not go all jason costa on em 5
ChevisN7 was not expecting much for this price but i was surprised have been using mine now for about months with no problems workmanship seems good does a good job not great of putting equal pressure on the strings but overall it has been a good product i also have a more expensive capo just like this one they look as though they come from the same factory more expensive is not always better 4

Top words

musical_instr_reviews %>% 
  select(reviewText) %>%
  unnest_tokens(output = word, 
                input = reviewText) %>% 
  anti_join(stop_words) %>%
  count(word, sort=TRUE) %>% 
  top_n(25) %>% 
  ggplot(aes(fct_reorder(word,n), n)) + 
  theme_tufte() +
  geom_col(fill = "#79CA32") + 
  xlab(NULL) + 
  coord_flip() + 
  ggtitle("Top words in Amazon Musical Instruments Reviews") +
  theme(plot.title = element_text(hjust = 0.5, size = 8)) 

Sentiment Word Cloud

library(wordcloud)
library(reshape2) # for acast function
set.seed(123) # for reproducibility

musical_instr_reviews %>%
  select(reviewText) %>%
  unnest_tokens(output=word,
                input=reviewText) %>%
  anti_join(stop_words) %>%
  inner_join(get_sentiments("bing")) %>%
  count(word, sentiment, sort = TRUE) %>%
  acast(word ~ sentiment, 
        value.var = "n", 
        fill = 0) %>%
  comparison.cloud(colors = c("blue","purple"),
                   scale = c(2,0.5),
                   max.words = 100,
                   title.size = 2)

 Group Activity 2


  • Please do the remaining problems in the class activity.
  • Submit to Gradescope on moodle when done!

10:00