diff --git a/tasks/basics.R b/tasks/basics.R index 84f9481..42068c5 100644 --- a/tasks/basics.R +++ b/tasks/basics.R @@ -1,29 +1,34 @@ # Create a numeric vector with values from 1 to 5 # Name the vector as "numeric_vector" - +numeric_vector <- c(1, 2, 3, 4, 5) # Function to calculate the sum of a numeric vector calculate_sum <- function(vector) { - # TODO: Complete this function to calculate the sum of 'vector' + # TODO: Complete this function to calculate the sum of 'vector' + sum(vector) } # Function to calculate the mean of a numeric vector calculate_mean <- function(vector) { - # TODO: Complete this function to calculate the mean of 'vector' + # TODO: Complete this function to calculate the mean of 'vector' + mean(vector) } # Function to calculate the median of a numeric vector calculate_median <- function(vector) { - # TODO: Complete this function to calculate the median of 'vector' + # TODO: Complete this function to calculate the median of 'vector' + median(vector) } # Function to calculate the standard deviation of a numeric vector calculate_std_deviation <- function(vector) { - # TODO: Calculate the standard deviation and round to 3 decimal places + # TODO: Calculate the standard deviation and round to 3 decimal places + round(sd(vector), 3) } # Function to calculate the range of a numeric vector calculate_range <- function(vector) { - # TODO: Complete this function to calculate the range of 'vector' - # To find the range of the vector just subtract the minimum value from the maximum value + # TODO: Complete this function to calculate the range of 'vector' + # To find the range of the vector just subtract the minimum value from the maximum value + max(vector) - min(vector) } diff --git a/tasks/dataset.R b/tasks/dataset.R index 7ea4d93..09c7e15 100644 --- a/tasks/dataset.R +++ b/tasks/dataset.R @@ -18,6 +18,8 @@ summary(mtcars) # - Display the plot. # Hint: You may want to use the ggplot2 package for creating and customizing the plot. +library(ggplot2) + # Task 3: Additional Customization # - Customize the bar plot further by adding labels, adjusting colors, and improving the appearance. # - Save the updated plot as a new image file. diff --git a/tasks/loops.R b/tasks/loops.R index 3f3572d..af17672 100644 --- a/tasks/loops.R +++ b/tasks/loops.R @@ -5,7 +5,13 @@ # - Check if each number is even and add it to the sum. # - Return the sum of even numbers. sum_even_numbers <- function(n) { - # Your code here + output <- 0 + for (i in 1:n) { + if (i %% 2 == 0) { + output <- output + i + } + } + output } # Task 2: Calculate Factorial Using a Loop @@ -14,7 +20,11 @@ sum_even_numbers <- function(n) { # - Use a loop to multiply the current number by the factorial. # - Return the factorial. factorial_using_loop <- function(n) { - # Your code here + output <- 1 + for (i in 1:n) { + output <- output * i + } + output } # Task 3: Reverse a String @@ -23,7 +33,11 @@ factorial_using_loop <- function(n) { # - Use a loop to reverse the characters in the string. # - Return the reversed string. reverse_string <- function(input_string) { - # Your code here + output <- "" + for (i in 1:nchar(input_string)) { + output <- paste(substr(input_string, i, i), output, sep = "") + } + output } # Task 4: Count the Number of Vowels @@ -33,7 +47,14 @@ reverse_string <- function(input_string) { # - Use a loop to iterate through each character and check if it's a vowel. # - Increment the vowel count and return it. count_vowels <- function(input_string) { - # Your code here + output <- 0 + vowels <- c("a", "e", "i", "o", "u") + for (i in 1:nchar(tolower(input_string))) { + if (substr(tolower(input_string), i, i) %in% vowels) { + output <- output + 1 + } + } + output } # Task 5: Generate a Fibonacci Sequence @@ -42,5 +63,9 @@ count_vowels <- function(input_string) { # - Use a loop to generate the subsequent Fibonacci numbers and add them to the vector. # - Return the first 'n' terms of the Fibonacci sequence. generate_fibonacci <- function(n) { - # Your code here + output <- c(1, 1) + for (i in 3:n) { + output <- append(output, output[i - 1] + output[i - 2]) + } + output }