Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

5 0 R E X E RC I S E S

A Practical Way to Learn R Programming

Daniel Christofis
INTRODUCTION

R is an open-source programming language and


environment made especially for data analysis and
statistical computing. R was created in the 1990s by
statisticians Ross Ihaka and Robert Gentleman at the
University of Auckland in New Zealand. Since then, it has
been widely used in both academia and business.

Key-Features:

1-Broad Statistical and Graphical Features: R offers a wide


range of graphical and statistical tools for data exploration,
analysis, and visualisation. Numerous statistical approaches
are covered by its extensive collection of packages.

2-Open Source and Cross-Platform: R is a cross-platform


programming language that is available for free and has an
active global developer and user community. It is
compatible with a number of operating systems, including
as Linux, macOS, and Windows.

3-R performs exceptionally well when it comes to data


analysis and manipulation. Users may efficiently clean,
filter, and analyse datasets with ease when they use
libraries such as tidyverse and dplyr.
4-Graphics & Visualisation: The language's ability to
visualise data is well known. For example, the ggplot2
package makes it simple for users to generate intricate and
visually appealing visualisations.

5-It's a Programming Language: R is a complete


programming language that supports procedural, functional,
and object-oriented programming paradigms, despite being
predominantly used as a statistical language. Because of
this, it can be used for a variety of activities that go beyond
statistics.

6-Package System: Users can access and exchange


datasets, methods, and extensions using R's package
system.

7-Reproducibility: Using scripts and notebooks, R


encourages reproducibility. Analysts can collaborate more
easily and ensure transparency by sharing and documenting
their workflows.
We hope you are enjoying your recent purchase from Amazon! We wanted to
take a moment to kindly remind you about the importance of leaving a review
for the product you bought. Your feedback is incredibly valuable to us and other
potential buyers.

By sharing your thoughts and experiences with the product, you help others
make informed decisions. Your review can be a guiding light for fellow
customers, ensuring they choose the best products for their needs.

We would greatly appreciate it if you could spare a few moments to write an


honest review. Your feedback helps us improve our products and services,
making it a win-win situation for everyone.

Thank you for being a valued customer and for considering writing a review. We
truly appreciate your support.

Introducing my personal blog. Stay connected!

https://premiumbookself.wordpress.com/
BEGINNER EXERCISES

1.1 Hello, R!
Write a simple R script that prints the message "Hello, R!" to
the console.
1.2 Basic Arithmetic Operations
Create a script that performs the following arithmetic
operations:
1.2.1 Addition of 5 and 7
1.2.2 Subtraction of 10 from 15
1.2.3 Multiplication of 3 by 4
1.2.4 Division of 20 by 5
1.3 Variable Declaration
Declare a variable called my_number and assign it the value
42. Print the value to the console.
1.4 Vector Creation
Create a numeric vector named my_vector containing the
numbers 1 to 5. Print the vector.
1.5 Vector Operations
Perform the following operations on the vector my_vector:
1.5.1 Multiply each element by 2
1.5.2 Add 3 to each element
Print the updated vector.
1.6 Logical Operations
Declare two variables, x and y, with values 5 and 10,
respectively. Check if x is less than y and print the result.
1.7 Data Types
Create variables of different data types: numeric, character,
logical, and print them.
1.8 Basic Data Frame
Create a data frame named my_dataframe with two
columns: "Name" and "Age," and enter information for three
people.
1.9 Indexing in Vectors
Given the vector letters_vector <- c("A", "B", "C", "D", "E"),
print the third element.
1.10 Subsetting Data Frames
From my_dataframe, select and print the row where the age
is greater than 25.
1.11 Conditional Statements
Write a script that checks if a variable num is even or odd
and prints an appropriate message.
1.12 For Loop
Use a for loop to print the numbers from 1 to 5.
1.13 While Loop
Implement a while loop that prints the squares of numbers
from 1 to 4.
1.14 Functions
Create a function called square that takes a number as an
argument and returns its square.
1.15 Packages
Install and load the "dplyr" package. Create a simple data
frame and use the filter function to subset it.
1.16 Reading Data
Read a CSV file named "data.csv" into a data frame called
my_data.
1.17 Plotting
Create a scatter plot with my_dataframe where "Age" is on
the x-axis and "Name" is on the y-axis.
1.18 Random Numbers
Generate a vector of 5 random numbers between 1 and 10.
1.19 Strings and Manipulation
Declare a character variable my_string with the value
"Hello, World!" and print its length.
1.20 Basic Error Handling
Write a script that attempts to divide a number by zero and
handle the resulting error with an informative message.
INTERMEDIATE
EXERCISES

2.1 Functions with Default Arguments


Create a function named power that takes two
arguments, base and exponent, with the default
exponent set to 2. The function should return the result
of raising the base to the exponent.
2.2 Data Frame Manipulation
Given a data frame named grades with columns
"Name," "Math," and "English," add a new column called
"Total" representing the sum of Math and English scores.
2.3 Conditional Data Frame Manipulation
From the grades data frame, create a new data frame
containing only the rows where the total score is greater
than 150.
2.4 List Manipulation
Create a list named my_list with the following elements:
a numeric vector, a character vector, and a logical
vector. Print each element separately.
2.5 List Operations
Add a new element to my_list containing a factor vector.
Print the updated list.
2.6 Apply Functions
Using the apply function, calculate the mean of each
column in the grades data frame.
2.7 Matrix Operations
Create a 3x3 matrix with random numeric values.
Calculate the sum of each row and each column.
2.8 Subsetting with Conditions
Extract rows from the grades data frame where both
Math and English scores are above the mean of their
respective columns.
2.9 File Writing
Write the grades data frame to a CSV file named
"student_grades.csv."
2.10 Error Handling in Functions
Modify the power function from question 2.1 to include
error handling. If the base is negative, print an error
message.
2.11 Loading External Data
Load the "iris" dataset from the datasets package.
Display the first 5 rows.
2.12 Data Frame Joining
Create two data frames, students and courses, with
columns "StudentID" and "CourseID" respectively.
Perform an inner join based on these columns.
2.13 Reshaping Data
Convert the grades data frame from wide to long format
using the melt function from the reshape2 package.
2.14 Time Series Creation
Create a time series object with daily values from
January 1, 2023, to January 10, 2023.
2.15 Plotting with ggplot2
Use ggplot2 to create a scatter plot of the "iris" dataset
with Sepal.Length on the x-axis and Sepal.Width on the
y-axis, color-coded by species.
2.16 String Manipulation with Regular Expressions
Given a character vector containing email addresses,
extract only the domain names (e.g., "gmail.com").
2.17 Creating a Shiny App
Install the shiny package and create a simple Shiny app
with a numeric input and a plot that displays the square
of the input.
2.18 Building a Function Pipeline
Create a function pipeline using the magrittr (%>%)
operator to take the mean, square, and then sum a
numeric vector.
2.19 Hierarchical Clustering
Apply hierarchical clustering to the "iris" dataset and
plot the resulting dendrogram.
2.20 Parallel Processing
Use the parallel package to parallelize the calculation of
the mean for each column in the grades data frame.
ADVANCED EXERCISES
3.1 Advanced Function
Create a function named fibonacci that generates
the Fibonacci sequence up to a specified term n.
3.2 Advanced Data Frame Manipulation
Given a data frame named sales with columns
"Date," "Product," and "Revenue," calculate the
rolling 7-day average revenue for each product.
3.3 Advanced List Manipulation
Create a nested list representing a 3x3x3 array of
numeric values. Use indexing to access and print
a specific element within the array.
3.4 Advanced Apply Functions
Utilize the purrr package to apply a custom
function that takes a numeric vector and returns
a list with the mean, standard deviation, and
skewness.
3.5 Advanced Matrix Operations
Implement matrix multiplication without using the
%*% operator. Define a function that takes two
matrices as input and returns their product.
3.6 Advanced Subsetting with Conditions
Extract rows from the sales data frame where the
revenue is at least 20% below the rolling average
for that product.
3.7 Advanced File Writing
Write the sales data frame to a JSON file with
nested structure, grouping data by product and
then by date.
3.8 Advanced Error Handling in Functions
Extend the fibonacci function from question 3.1 to
handle invalid inputs (e.g., negative numbers)
gracefully and print an informative error
message.
3.9 Advanced Machine Learning
Apply a support vector machine (SVM) classifier
to the "iris" dataset, predicting the species based
on the other features.
3.10 Advanced Parallel Processing
Use the future package to parallelize the training
of multiple decision trees using the randomForest
algorithm on the "iris" dataset.
BEGINNER SOLUTIONS
# 1.1 Hello, R!
print("Hello, R!")
# 1.2 Basic Arithmetic Operations
# 1.2.1 Addition
result_addition <- 5 + 7
print(result_addition)

# 1.2.2 Subtraction
result_subtraction <- 15 - 10
print(result_subtraction)

# 1.2.3 Multiplication
result_multiplication <- 3 * 4
print(result_multiplication)

# 1.2.4 Division
result_division <- 20 / 5
print(result_division)

# 1.3 Variable Declaration


my_number <- 42
print(my_number)
# 1.4 Vector Creation
my_vector <- c(1, 2, 3, 4, 5)
print(my_vector)
# 1.5 Vector Operations
# 1.5.1 Multiply each element by 2
my_vector <- my_vector * 2

# 1.5.2 Add 3 to each element


my_vector <- my_vector + 3
print(my_vector)
# 1.6 Logical Operations
x <- 5
y <- 10
result_logical <- x < y
print(result_logical)
# 1.7 Data Types
numeric_var <- 42
character_var <- "Hello, R!"
logical_var <- TRUE
print(numeric_var)
print(character_var)
print(logical_var)
# 1.8 Basic Data Frame
my_dataframe <- data.frame(
Name = c("Person1", "Person2", "Person3"),
Age = c(25, 30, 35)
)
print(my_dataframe)
# 1.9 Indexing in Vectors
letters_vector <- c("A", "B", "C", "D", "E")
print(letters_vector[3])
# 1.10 Subsetting Data Frames
#(include the code from 1.8)
selected_rows <- my_dataframe[my_dataframe$Age > 25, ]
print(selected_rows)
# 1.11 Conditional Statements
num <- 7
if (num %% 2 == 0) {
print("Even")
} else {
print("Odd")
}
# 1.12 For Loop
for (i in 1:5) {
print(i)
}
# 1.13 While Loop
num <- 1
while (num <= 4) {
print(num^2)
num <- num + 1
}
# 1.14 Functions
square <- function(x) {
return(x^2)
}
print(square(3))
# 1.15 Packages
install.packages("dplyr")
library(dplyr)
my_data <- data.frame(
Name = c("John", "Alice", "Bob"),
Age = c(22, 28, 30)
)
filtered_data <- filter(my_data, Age > 25)
print(filtered_data)
# 1.16 Reading Data
my_data <- read.csv("data.csv")
print(my_data)
# 1.17 Plotting
#(include the code from 1.8)
# Scatter plot
plot(my_dataframe$Age, seq_along(my_dataframe$Name),
main = "Scatter Plot of Age vs Name",
xlab = "Age", ylab = "Name",
yaxt = "n",
yaxt.labels = my_dataframe$Name)
# Add labels to y-axis
axis(2, at = seq_along(my_dataframe$Name), labels =
my_dataframe$Name, las = 1)
# 1.18 Random Numbers
random_numbers <- sample(1:10, 5)
print(random_numbers)
# 1.19 Strings and Manipulation
my_string <- "Hello, World!"
string_length <- nchar(my_string)
print(string_length)
# 1.20 Basic Error Handling
numerator <- 10
denominator <- 0
result <- tryCatch({
numerator / denominator
}, error = function(e) {
print(paste("Error:", e))
})

Reference Output
#1-1
[1] "Hello, R!"

#1-2-1
[1] 12

#1-2-2
[1] 5

#1-2-3
[1] 12

#1-2-4
[1] 4

#1-3
[1] 42

#1-4
[1] 1 2 3 4 5

#1-5
[1] 5 7 9 11 13

#1-6
[1] TRUE

#1-7
> numeric_var <- 42
> character_var <- "Hello, R!"
> logical_var <- TRUE
> print(numeric_var)
[1] 42
> print(character_var)
[1] "Hello, R!"
> print(logical_var)
[1] TRUE

#1-8
Name Age
1 Person1 25
2 Person2 30
3 Person3 35
#1-9
[1] "C"

#1-10
Name Age
2 Person2 30
3 Person3 35
#1-11
[1] "Odd"

#1-12
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

#1-13
[1] 1
[1] 4
[1] 9
[1] 16

#1-14
[1] 9

#1-15
Name Age
1 Alice 28
2 Bob 30

#1-16
First, create a csv file for example:
Student,Math,English,Science
Alice,78,92,87
Bob,95,76,60
Charlie,82,88,95

Student Math English Science


1 Alice 91 77 98
2 Bob 76 65 78
3 Charlie 77 69 86
#1-17

#1-18
[1] 3 9 2 8 4

#1-19
[1] 13

#1-20
> numerator <- 10
> denominator <- 0
> result <- tryCatch({
+ numerator / denominator
+ }, error = function(e) {
+ print(paste("Error:", e))
+ })
INTERMEDIATE
SOLUTIONS
# 2.1 Functions with Default Arguments
power <- function(base, exponent = 2) {
return(base^exponent)
}
print(power(3)) # Should print 9
# 2.2 Data Frame Manipulation
grades <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Math = c(60, 45, 92),
English = c(88, 20, 95)
)
# Data frame manipulation
grades$Total <- grades$Math + grades$English
print(grades)
# 2.3 Conditional Data Frame Manipulation (include the
code from 2.2)
filtered_grades <- grades[grades$Total > 150, ]
print(filtered_grades)
# 2.4 List Manipulation
my_list <- list(
numeric_vector = c(1, 2, 3),
character_vector = c("a", "b", "c"),
logical_vector = c(TRUE, FALSE, TRUE)
)
print(my_list)
# 2.5 List Operations
my_list$factor_vector <- factor(c("low", "medium", "high"))
print(my_list)
# 2.6 Apply Functions (include the code from 2.2)
column_means <- apply(grades[, c("Math", "English",
"Total")], 2, mean)
print(column_means)
# 2.7 Matrix Operations
matrix_data <- matrix(rnorm(9), nrow = 3)
row_sums <- apply(matrix_data, 1, sum)
col_sums <- apply(matrix_data, 2, sum)
print(row_sums)
print(col_sums)
# 2.8 Subsetting with Conditions
(include the code from 2.2)
subset_grades <- grades[grades$Math >
mean(grades$Math) & grades$English >
mean(grades$English), ]
print(subset_grades)
# 2.9 File Writing
write.csv(grades, "student_grades.csv")
# 2.10 Error Handling in Functions
power <- function(base, exponent = 2) {
if (base < 0) {
stop("Base must be non-negative.")
}
return(base^exponent)
}
print(power(3)) # Should print 9
# Uncomment the line below to test the error handling
# print(power(-2))
# 2.11 Loading External Data
data(iris)
print(head(iris, 5))
# 2.12 Data Frame Joining
# Creating data frames
students <- data.frame(StudentID = c(1, 2, 3), Name =
c("Alice", "Bob", "Charlie"))
courses <- data.frame(CourseID = c(1, 2, 3), Course =
c("Math", "English", "Science"))
# Merging data frames
joined_data <- merge(students, courses, by.x =
"StudentID", by.y = "CourseID")
# Printing the result
print(joined_data)
# 2.13 Reshaping Data
# Sample data creation
grades <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Math = c(60, 45, 92),
English = c(88, 20, 95),
Total = c(148, 65, 187)
)
# Install and Load reshape2 package
install.packages("reshape2")
library(reshape2)
# Melt the data frame
melted_grades <- melt(grades, id.vars = "Name",
measure.vars = c("Math", "English", "Total"))
print(melted_grades)
# 2.14 Time Series Creation
start_date <- as.Date("2023-01-01")
end_date <- as.Date("2023-01-10")
time_series <- seq(start_date, end_date, by = "day")
print(time_series)
# 2.15 Plotting with ggplot2
install.packages("ggplot2")
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color =
Species)) +
geom_point()
# 2.16 String Manipulation with Regular Expressions
email_addresses <- c("user1@gmail.com",
"user2@yahoo.com", "user3@hotmail.com")
domain_names <- gsub(".*@(.*)", "\\1", email_addresses)
print(domain_names)
# 2.17 Creating a Shiny App
install.packages("shiny")
library(shiny)
ui <- fluidPage(
numericInput("input_num", "Enter a number:", value = 5),
plotOutput("output_plot")
)
server <- function(input, output) {
output$output_plot <- renderPlot({
plot(input$input_num^2)
})
}
shinyApp(ui, server)
# 2.18 Building a Function Pipeline
install.packages("magrittr")
library(magrittr)
numeric_vector <- c(1, 2, 3, 4, 5)
result <- numeric_vector %>%
mean() %>%
`^`(2) %>%
sum()
print(result)
# 2.19 Hierarchical Clustering
iris_subset <- iris[, 1:4]
hc_result <- hclust(dist(iris_subset))
plot(hc_result)
# 2.20 Parallel Processing
library(parallel)
grades <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Math = c(60, 45, 92),
English = c(88, 20, 95),
Total = c(148, 65, 187)
)
grades_parallel <- mclapply(grades, mean)
print(grades_parallel)

Reference Output

#2-1
[1] 9

#2-2
Name Math English Total
1 Alice 60 88 148
2 Bob 45 20 65
3 Charlie 92 95 187

#2-3
Name Math English Total
3 Charlie 92 95 187

#2-4
$numeric_vector
[1] 1 2 3

$character_vector
[1] "a" "b" "c"

$logical_vector
[1] TRUE FALSE TRUE

#2-5
[1] low medium high
Levels: high low medium
#2-6
Math English Total
64.00000 72.66667 136.66667

#2-7
> print(row_sums)
[1] 0.4316654 -1.2885164 -0.1953087
> print(col_sums)
[1] -2.0720193 0.6168985 0.4029610

#2-8
Name Math English
3 Charlie 92 95

#2-9
This code will create a CSV file named "student_grades.csv" in your working
directory with the specified data.

#2-10
Error in power(-2) : Base must be non-negative.

#2-11
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa

#2-12
StudentID Name Course
1 1 Alice Math
2 2 Bob English
3 3 Charlie Science
#2-13
Name variable value
1 Alice Math 60
2 Bob Math 45
3 Charlie Math 92
4 Alice English 88
5 Bob English 20
6 Charlie English 95
7 Alice Total 148
8 Bob Total 65
9 Charlie Total 187

#2-14
[1] "2023-01-01" "2023-01-02" "2023-01-03" "2023-01-04" "2023-01-05"
[6] "2023-01-06" "2023-01-07" "2023-01-08" "2023-01-09" "2023-01-10"

#2-15

#2-16
[1] "gmail.com" "yahoo.com" "hotmail.com"

#2-17

#2-18
[1] 9

#2-19
#2-20

$Name

[1] NA

$Math

[1] 65.66667

$English

[1] 67.66667
$Total

[1] 133.3333
ADVANCED SOLUTIONS
# 3.1 Advanced Function
# Function to generate the Fibonacci sequence up to term n
fibonacci <- function(n) {
fib_seq <- numeric(n)
fib_seq[1:2] <- 1
for (i in 3:n) {
fib_seq[i] <- fib_seq[i - 1] + fib_seq[i - 2]
}
return(fib_seq)
}
print(fibonacci(10)) # Should print the first 10 Fibonacci
numbers
# 3.2 Advanced Data Frame Manipulation
library(dplyr)
# Create data frame with consistent number of rows for
each product
sales <- data.frame(
Date = rep(seq(as.Date("2023-01-01"), as.Date("2023-01-
31"), by = "days"), each = 2),
Product = rep(c("A", "B"), each = 31),
Revenue = rnorm(62, mean = 50, sd = 10)
)
# Calculate Rolling_Avg
sales <- sales %>%
group_by(Product) %>%
arrange(Date) %>% # Ensure proper ordering
mutate(Rolling_Avg = zoo::rollmean(Revenue, k = 7, fill =
NA, align = "right"))
# Print the result
print(sales)
# 3.3 Advanced List Manipulation
nested_list <- replicate(3, replicate(3, rnorm(3)))
print(nested_list[[2]][[1]][1]) # Accessing a specific
element
# 3.4 Advanced Apply Functions
install.packages("purrr")
install.packages("e1071")
library(purrr)
custom_function <- function(x) {
return(list(mean = mean(x), sd = sd(x), skewness =
e1071::skewness(x)))
}
numeric_vector <- rnorm(100)
result_list <- map(numeric_vector, custom_function)
print(result_list)
# 3.5 Advanced Matrix Operations
matrix_multiply <- function(mat1, mat2) {
if (ncol(mat1) != nrow(mat2)) {
stop("Number of columns in mat1 must equal the number
of rows in mat2.")
}
result_mat <- matrix(0, nrow = nrow(mat1), ncol =
ncol(mat2))
for (i in 1:nrow(mat1)) {
for (j in 1:ncol(mat2)) {
result_mat[i, j] <- sum(mat1[i, ] * mat2[, j])
}
}
return(result_mat)
}
mat1 <- matrix(1:6, nrow = 2)
mat2 <- matrix(7:12, ncol = 2)
print(matrix_multiply(mat1, mat2))
# 3.6 Advanced Subsetting with Conditions(using the “zoo”
package)
install.packages("zoo")
library(dplyr)
library(zoo)
# Create data frame with consistent number of rows for
each product
sales <- data.frame(
Date = rep(seq(as.Date("2023-01-01"), as.Date("2023-01-
31"), by = "days"), each = 2),
Product = rep(c("A", "B"), each = 31),
Revenue = rnorm(62, mean = 50, sd = 10)
)
# Calculate Rolling_Avg
sales <- sales %>%
group_by(Product) %>%
arrange(Date) %>% # Ensure proper ordering
mutate(Rolling_Avg = zoo::rollmean(Revenue, k = 7, fill =
NA, align = "right"))
# Create a new data frame for filtering
sales_filtered <- sales %>%
filter(Revenue <= 0.8 * Rolling_Avg)
# Print the result
print(sales_filtered)
# 3.7 Advanced File Writing
library(jsonlite)
json_data <- toJSON(sales, pretty = TRUE)
writeLines(json_data, "sales_data.json")
# 3.8 Advanced Error Handling in Functions
fibonacci <- function(n) {
if (!is.numeric(n) || n < 1) {
stop("Input must be a positive numeric value.")
}
fib_seq <- numeric(n)
fib_seq[1:2] <- 1
for (i in 3:n) {
fib_seq[i] <- fib_seq[i - 1] + fib_seq[i - 2]
}
return(fib_seq)
}
print(fibonacci(-5)) # Should trigger an error
# 3.9 Advanced Machine Learning
library(e1071)
iris_svm <- svm(Species ~ ., data = iris)
print(summary(iris_svm))
# 3.10 Advanced Parallel Processing
install.packages("future")
install.packages("furrr")
install.packages("randomForest")
library(randomForest)
library(furrr)
# Set up parallel processing
future::plan("multisession", workers = 5)
furrr::setup_furrr()
# Create random forests with different seeds in parallel
iris_rf <- furrr::future_map(1:5, ~ {
set.seed(.)
randomForest(Species ~ ., data = iris)
})
# Print the random forests
print(iris_rf)
Reference Output

#3-1
[1] 1 1 2 3 5 8 13 21 34 55

#3-2
Date Product Revenue
1 2023-01-01 A 53.35887
2 2023-01-01 A 63.90985
3 2023-01-02 A 50.64605
4 2023-01-02 A 38.39446
5 2023-01-03 A 51.66800
6 2023-01-03 A 50.33330
7 2023-01-04 A 58.92325
8 2023-01-04 A 44.40268
9 2023-01-05 A 62.85888
10 2023-01-05 A 66.73350
11 2023-01-06 A 57.52942

#3-3
[1] -0.2010459

#3-4
[[1]]
[[1]]$mean
[1] 0.699673

[[1]]$sd
[1] NA

[[1]]$skewness
[1] NaN

[[2]]
[[2]]$mean
[1] 0.5873104

[[2]]$sd
[1] NA

[[2]]$skewness
[1] NaN

[[3]]
[[3]]$mean
[1] -0.2093998

[[3]]$sd
[1] NA

[[3]]$skewness
[1] NaN

#3-5
[,1] [,2]
[1,] 76 103
[2,] 100 136

#3-6
# A tibble: 2 × 4
# Groups: Product [2]
Date Product Revenue Rolling_Avg
<date> <chr> <dbl> <dbl>
1 2023-01-13 A 40.1 52.5
2 2023-01-23 B 27.4 48.5

#3-7
JSON file is created:
[
{
"Date": "2023-01-01",
"Product": "A",
"Revenue": 58.7994
},
{
"Date": "2023-01-01",
"Product": "A",
"Revenue": 49.3781
},
{
"Date": "2023-01-02",
"Product": "A",
"Revenue": 41.3328
},
{
"Date": "2023-01-02",
"Product": "A",
"Revenue": 47.1283
},
{
"Date": "2023-01-03",
"Product": "A",
"Revenue": 60.7428
},
{
"Date": "2023-01-03",
"Product": "A",
"Revenue": 38.9607
},

#3-8
Error in fibonacci(-5) : Input must be a positive numeric value.

#3-9
Call:
svm(formula = Species ~ ., data = iris)

Parameters:
SVM-Type: C-classification
SVM-Kernel: radial
cost: 1

Number of Support Vectors: 51

( 8 22 21 )

Number of Classes: 3

Levels:
setosa versicolor virginica

#3-10

[[1]]

Call:

randomForest(formula = Species ~ ., data = iris)

Type of random forest: classification

Number of trees: 500

No. of variables tried at each split: 2

OOB estimate of error rate: 4.67%

Confusion matrix:

setosa versicolor virginica class.error

setosa 50 0 0 0.00

versicolor 0 47 3 0.06

virginica 0 4 46 0.08

[[2]]

Call:

randomForest(formula = Species ~ ., data = iris)

Type of random forest: classification

Number of trees: 500

No. of variables tried at each split: 2

OOB estimate of error rate: 4%

Confusion matrix:

setosa versicolor virginica class.error

setosa 50 0 0 0.00

versicolor 0 47 3 0.06
virginica 0 3 47 0.06

[[3]]

Call:

randomForest(formula = Species ~ ., data = iris)

Type of random forest: classification

Number of trees: 500

No. of variables tried at each split: 2

OOB estimate of error rate: 4.67%

Confusion matrix:

setosa versicolor virginica class.error

setosa 50 0 0 0.00

versicolor 0 47 3 0.06

virginica 0 4 46 0.08

[[4]]

Call:

randomForest(formula = Species ~ ., data = iris)

Type of random forest: classification

Number of trees: 500

No. of variables tried at each split: 2

OOB estimate of error rate: 4%

Confusion matrix:

setosa versicolor virginica class.error

setosa 50 0 0 0.00

versicolor 0 47 3 0.06

virginica 0 3 47 0.06
[[5]]

Call:

randomForest(formula = Species ~ ., data = iris)

Type of random forest: classification

Number of trees: 500

No. of variables tried at each split: 2

OOB estimate of error rate: 4.67%

Confusion matrix:

setosa versicolor virginica class.error

setosa 50 0 0 0.00

versicolor 0 47 3 0.06

virginica 0 4 46 0.08

You might also like