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
8 changes: 7 additions & 1 deletion tasks/basics.R
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
# 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'
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'
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'
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
round(sd(vector),digits=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
range <- max(vector)-min(vector)
print(range)
}
25 changes: 25 additions & 0 deletions tasks/dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ data(mtcars)
str(mtcars)
summary(mtcars)

#installing and loading the ggplot2 package
install.packages("ggplot2")
library(ggplot2)

# Task 2: Create a bar plot for average miles per gallon (mpg)
# - Calculate the average miles per gallon (mpg) for each car model.
# - Create a bar plot to visualize the average mpg for each car model.
Expand All @@ -18,21 +22,42 @@ summary(mtcars)
# - Display the plot.
# Hint: You may want to use the ggplot2 package for creating and customizing the plot.

#There is no need to calculate the mean of mpg value for each car model since each car model has only one mpg value
p <- ggplot(data=mtcars, aes(x=rownames(mtcars), y=mtcars$mpg)) + geom_bar(stat="identity", width=1.0, color='black', fill='purple')
p <- p+theme(axis.text.x = element_text(angle = 90, hjust=1))
ggsave("mtcars_barplot1.pdf", p)
print(p)

# 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.
# - Display the updated plot.
p <- p + labs(title="AVERAGE MPG OF CAR MODELS", x="Car Model", y="Miles per Gallon(mpg)")+theme(plot.title = element_text(colour = "red"))
ggsave("mtcars_plot2.pdf", p)
print(p)

# Task 4: Export Data
# - Save the modified "mtcars" dataset to a CSV file for further analysis.
# - Use the "write.csv" function to export the dataset.
write.csv(mtcars, "mtcars.csv") #The data set is not modified since no changes were made

# Task 5: Summary and Reporting
# - Summarize the findings and observations from the data visualization.
# - Create a brief report explaining the insights gained from the visualization.
#Summary--
# The mpg values among the cars are diverse, ranging from just above 10 upto 33.9 miles per gallon.
# 'Toyota 'Corolla' is the most fuel efficient while 'Cadillac Fleetwood' and 'Lincoln Continental' the least.

# Task 6: Bonus Challenge (5 points)
# - Create a second type of plot (e.g., scatterplot, boxplot, or line chart) to explore another aspect of the "mtcars" dataset.
# - Customize the plot.
# - Save the plot as an image file.
# - Display the plot.

# Creating a scatterplot of Car weight vs mpg
s_plot <- ggplot(mtcars, aes(x = wt, y = mpg)) +geom_point(color = 'red', size = 3) + labs(title = "Scatterplot of Car Weight vs. Miles Per Gallon",
x = "Car Weight",
y = "Miles Per Gallon") + theme_minimal()

ggsave("scatterplot_mtcars.pdf", s_plot, width = 7, height = 7, units = "in")
print(s_plot)
32 changes: 31 additions & 1 deletion tasks/loops.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
# - Return the sum of even numbers.
sum_even_numbers <- function(n) {
# Your code here
sum_even <-0
for (i in 1:n) {
if (i %% 2 == 0) {
sum_even <- sum_even + i
}
}
return (sum_even)
}

# Task 2: Calculate Factorial Using a Loop
Expand All @@ -15,6 +22,11 @@ sum_even_numbers <- function(n) {
# - Return the factorial.
factorial_using_loop <- function(n) {
# Your code here
factorial <- 1
for (i in 1:n){
factorial <- factorial*i
}
return (factorial)
}

# Task 3: Reverse a String
Expand All @@ -24,7 +36,12 @@ factorial_using_loop <- function(n) {
# - Return the reversed string.
reverse_string <- function(input_string) {
# Your code here
}
rev_string <-""
for (i in nchar(input_string):1) {
rev_string <- paste0(rev_string, substr(input_string, i, i))
}
return (rev_string)
}

# Task 4: Count the Number of Vowels
# - Define a function called 'count_vowels' that counts the number of vowels in a string.
Expand All @@ -34,6 +51,13 @@ reverse_string <- function(input_string) {
# - Increment the vowel count and return it.
count_vowels <- function(input_string) {
# Your code here
vowel_count <- 0
for (char in strsplit(input_string, NULL)[[1]]) {
if (char %in% c("a", "e", "i", "o", "u","A","E","I","O","U")) {
vowel_count <- vowel_count + 1
}
}
return(vowel_count)
}

# Task 5: Generate a Fibonacci Sequence
Expand All @@ -43,4 +67,10 @@ count_vowels <- function(input_string) {
# - Return the first 'n' terms of the Fibonacci sequence.
generate_fibonacci <- function(n) {
# Your code here
fibonacci_sequence <- c(1, 1)
for (i in 3:n) {
next_fibonacci <- fibonacci_sequence[i - 1] + fibonacci_sequence[i - 2]
fibonacci_sequence <- c(fibonacci_sequence, next_fibonacci)
}
return(fibonacci_sequence[1:n])
}