Statistical Programming - R

Assignment 1

1. Create a function named pass_check that takes a number for the argument grade and returns “pass” if the number is equal or greater than 5 and “fail” if it’s lower.

pass_check<-function(grade) {
  if (grade>=5){
    print("pass")}
  else {
    print("fail")
  }
}
pass_check1(6)
pass_check1(2)

1.1 Try to make it more reliable by creating an error whenever the number is negative or greater than 10.

pass_check1<-function(grade) {
  if(grade>10 | grade<0) {
    stop('Valid grades are between 0-10!')
  }
  if (grade>=5){
    print("pass")}
  else {
    print("fail")
  }
}

pass_check1(-3)
pass_check1(14)
pass_check1(9)
pass_check1(3)

1.2 Add two aditional arguments, on_time and optional_part. When on_time is FALSE, then 1 point should be substracted from the number entered, and when optional_part is TRUE, then 1 point should be added to the number entered. Then return “pass” when the final grade is greater than 5 and “fail” when it’s lower. As a clarifying example, when you call the function with pass_check(grade=5,on_time = F, optional_part = F) the result should be “fail”, as 5 - 1 + 0 = 4, and when you call the function with pass_check(grade=4,on_time = T, optional_part = T) it should return “pass”, as 4 - 0 + 1 = 5. Besides, in this case, the final grade is allowed to be -1 or 11.

pass_check2<-function(grade,on_time,optional_part) {
  if(grade>10 | grade<0) {
    stop('Valid grades are between 0-10!')
  } 
  if(on_time==F) {
    grade<-grade-1 
    }
  if(optional_part==T) {
    grade<-grade+1 
    } 
  if (grade>=5){  
    return("pass") 
    }
  else {
    return("fail")
  } 
}

pass_check2(4,T,T)
pass_check2(4,T,F)
pass_check2(5,F,F)
pass_check2(5,F,T)

1.3 Create a vector grades with numbers 1 to 10. Pass the function to each element of the vector as the argument grade, with values for on_time and optional_part always fixed at TRUE and FALSE respectively.

grades<-c(1:10)
for(i in 1:length(grades)){
  print(pass_check(grades[i], on_time = T,optional_part = F))
  }

-OR with a sapply

grades<-c(1:10)
sapply(grades,pass_check,on_time=T,optional_part=F)

2. Read the df_states.csv from local, with the argument stringsAsFactors=F. Manage to automatically coerce all character variables to factor ones, without naming any specific column. This function should work with any data.frame no matter the dimension, number, names or position of character variables.

df_states<-read.csv("~/Documents/IE/Classes/Building the Toolkit/Share - R/df_states.csv",stringsAsFactors = F,sep = ";")

View(df_states)

str(df_states)

-With afor loop

factorizing_dfs<-function(example_df){
for(i in 1:ncol(example_df)){
   if(is.character(example_df[,i])){
     example_df[,i]<-as.factor(example_df[,i])
     }
  } 
  return(example_df)
}

df_states2<- factorizing_dfs(df_states)

str(df_states)

-With sapply

factorizing_dfs_apply<-function(df_states){
  char_cols<-sapply(df_states,is.character)
  df_states[,char_cols] <- lapply(df_states[,char_cols],as.factor)
  return(df_states)
}

df_states3<-factorizing_dfs_apply(df_states)

str(df_states3)