Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions tasks/basics.R
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 2 additions & 0 deletions tasks/dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
35 changes: 30 additions & 5 deletions tasks/loops.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}