The function:
convert_temperature <- function(temperature,F_to_C){
if (F_to_C == FALSE) {
new_temperature = (temperature * 9 / 5) + 32
}
else{
new_temperature = (temperature - 32) * 5 / 9
}
return(new_temperature)
}
Execution to convert from F to C:
convert_temperature(temperature = 86,F_to_C = TRUE)
## [1] 30
Execution to convert from C to F:
convert_temperature(temperature = 30,F_to_C = FALSE)
## [1] 86
The function:
calculate_future_value <- function(investment, interest, duration_in_years){
future_val = investment * (1 + interest)^duration_in_years
return(future_val)
}
Execution for 100 units of investments 7% interest rate over 5 years:
calculate_future_value(investment = 100, interest = 0.07, duration_in_years = 5)
## [1] 140.2552
The function:
generate_hex_code <- function(n){
num_vals <- c(0:9)
letter_vals <- c("a","b","c","d","e","f")
all_vals <- append(num_vals, letter_vals)
hex_code <- ""
hex_array <- c()
for (y in 1:n) {
for (x in 1:6) {
hex_code <- paste0(hex_code, sample(all_vals,1))
}
hex_code <- paste0("#",hex_code)
hex_array[y] <- hex_code
hex_code <- ""
}
return(hex_array)
}
Color hex code generation:
generate_hex_code(n=3)
## [1] "#3d68a5" "#32983b" "#941fbe"
The function:
get_prob_dice <- function(six_count, throw_count){
probability_rate <- dbinom(six_count, size=throw_count, prob=1/6)
return(probability_rate)
}
Probability of getting 3 sixes in 5 throws:
get_prob_dice(3,5)
## [1] 0.03215021
The function:
rsp_game <- function(choice){
possible_choices <- c("rock","scissors","paper")
func_choice <- sample(1:3, 1)
my_choice <- possible_choices[func_choice]
if (my_choice == choice) {
return("I choose the same. Tie!")
}
else if(my_choice == "rock" & choice == "scissors"){
return("You lost, I choosed rock.")
}
else if(my_choice == "rock" & choice == "paper"){
return("You won, I choosed rock.")
}
else if(my_choice == "scissors" & choice == "rock"){
return("You won, I choosed scissors")
}
else if(my_choice == "scissors" & choice == "paper"){
return("You lost, I choosed scissors")
}
else if(my_choice == "paper" & choice == "rock"){
return("You lost, I choosed paper")
}
else if(my_choice == "paper" & choice == "scissors"){
return("You won, I choosed paper")
}
}
Sample game results:
rsp_game("rock")
## [1] "I choose the same. Tie!"
rsp_game("scissors")
## [1] "You won, I choosed paper"
rsp_game("paper")
## [1] "You lost, I choosed scissors"
Back to my Progress Journal