1. Write a function to calculate the age of a person. Give an example to test your function.

Hint: Similar Function

age <- function(x)
{
  r = 2020-x
  print(paste0('You are ', r,' years old.'))
}
age(1978)
## [1] "You are 42 years old."

  1. Write the following function. Give an example to test your function.

Hint: Similar Function

number <- function(x)
{
  if((x %% 2) == 0)
  {
    print('You input an even number!')
  }
  else
  {
    print('You input an odd number!')
  }
}
number(5)
## [1] "You input an odd number!"
number(10)
## [1] "You input an even number!"

  1. Write the following function. Give an example to test your function.

Hint: Similar Function

impute <- function(x)
{
  if(sum(is.na(x)>0))
  {
    mean <-  mean(x,na.rm=TRUE)
    library(tidyr)
    x <- replace_na(x, mean)
  }
  return(x)
}

a <- c(1,10,2,5,NA,7,18,NA,4,1)
impute(a)
##  [1]  1 10  2  5  6  7 18  6  4  1
b <- c(1:10)
impute(b)
##  [1]  1  2  3  4  5  6  7  8  9 10

  1. Write the following function. Give an example to test your function.

Hint: Combine the function in Question 3 and this function

impute1 <- function(x)
{
  if(sum(is.na(x)>0) && is.numeric(x))
  {
    mean <-  mean(x,na.rm=TRUE)
    library(tidyr)
    x <- replace_na(x, mean)
  }
  if(sum(is.na(x)>0) && !is.numeric(x))
  {
    mode <- names(sort(-table(x)))[1]
    x <- replace_na(x, mode)
  }
  return(x)
}

a <- c(1,10,2,5,NA,7,18,NA,4,1)
impute1(a)
##  [1]  1 10  2  5  6  7 18  6  4  1
b <- c(1:10)
impute(b)
##  [1]  1  2  3  4  5  6  7  8  9 10
c <- c("chair","desk","chair","table",NA,"desk","chair")
impute1(c)
## [1] "chair" "desk"  "chair" "table" "chair" "desk"  "chair"

  1. Write the following function. Give examples to test your function.

Hint:

viz <- function(d)
{
  if(is.numeric(d[[1]])&!is.numeric(d[[2]]))
  {
    library(ggplot2)
    d %>% ggplot(aes(x = d[[1]], fill = d[[2]]))+
      geom_boxplot()+
      labs(x = names(d)[1], fill = names(d)[2])
  }
  else if (!is.numeric(d[[1]])&is.numeric(d[[2]]))
  {
    d %>% ggplot(aes(x = d[[2]], fill = d[[1]]))+
      geom_boxplot()+
      labs(x = names(d)[2], fill = names(d)[1])
  }
  else 
  {
    print('This function cannot visualize your data.')
  }
}

library(readr)
library(dplyr)
df <- read_csv('titanic.csv')
d <- df %>% select(Age,Sex)
viz(d)

df$Pclass <- factor(df$Pclass)
e <- df %>% select(Sex,Pclass)
viz(e)
## [1] "This function cannot visualize your data."

  1. Combine the function in Question 5 and the function in this example (Link) to have a function that can handle all possible cases.
viz1 <- function(d)
{
  if(is.numeric(d[[1]])&!is.numeric(d[[2]]))
  {
    library(ggplot2)
    d %>% ggplot(aes(x = d[[1]], fill = d[[2]]))+
      geom_boxplot()+
      labs(x = names(d)[1], fill = names(d)[2])
  }
  else if (!is.numeric(d[[1]])&is.numeric(d[[2]]))
  {
    d %>% ggplot(aes(x = d[[2]], fill = d[[1]]))+
      geom_boxplot()+
      labs(x = names(d)[2], fill = names(d)[1])
  }
  else if(is.numeric(d[[1]])&is.numeric(d[[2]]))
  {
    d %>% ggplot(aes(x = d[[1]], y = d[[2]]))+
      geom_point()+
      labs(x = names(d)[1], y = names(d)[2])
  }
  else if(!(is.numeric(d[[1]])|is.numeric(d[[2]])))
  {
    d %>% ggplot(aes(x = d[[1]], fill = d[[2]]))+
      geom_bar(position = 'dodge')+
      labs(x = names(d)[1], fill = names(d)[2])
  }
}

viz1(d)

viz1(e)