Input: a number, x, (year born)
Output: Print out “You are r age”. Where r is the age of the person, i.e. 2020 - x.
Hint: Similar Function
age <- function(x)
{
r = 2020-x
print(paste0('You are ', r,' years old.'))
}
age(1978)
## [1] "You are 42 years old."
Input: a number
Output: print out: “You input an even number!” if the number is event, or “You input an odd number!” otherwise.
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!"
Input: a numeric vector
Output:
if the input vector has missing values: return the input vector with missing values replaced by mean
if the input vector has no missing value: return the same input vector
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
Input: a vector x
Output: The vector x where the missing values replaced by the mean (if x is numeric) or the mode (if x is non-numeric). If x does not have missing value, return the same vector x.
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"
Input: A data frame of two variables x and y
Output:
A boxplot of x by y (aes(x = x, fill = y)
) if x is numeric and y is non-numeric
A boxplot of y by x (aes(x = x, fill = y)
) if y is numeric and x is non-numeric
print out ‘This function cannot visualize your data’ otherwise
Hint:
You can refer to this slide to plot a boxplot: https://bryantstats.github.io/math421/slides/6_viz.html#36
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."
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)