Question
Can someone correct my R script for the following errors while keeping it as intact as possible Error in `mutate()`: ! Problem while computing `year
Can someone correct my R script for the following errors while keeping it as intact as possible Error in `mutate()`:
! Problem while computing `year = year(date)`.
Caused by error in `as.POSIXlt.default()`:
! do not know how to convert 'x' to class POSIXlt
Run `rlang::last_error()` to see where the error occurred.
Error in group_by(., year) : object 'nottem_tidy_df' not found
Error in ggplot(average_temp_by_year_df, aes(year, avg_temp)) :
object 'average_temp_by_year_df' not found
Error in ggplot(class_counts, aes(x = Class, y = prop_survived)) :
object 'class_counts' not found
# Title: My Name - RStudio Importing and Visualizing Data
# Check if pacman is installed and install it if not
if (!require("pacman")) install.packages("pacman")
print("Pacman Installed")
# Use pacman to load/install required packages
pacman::p_load(pacman, datasets, tidyverse, tsibble, lubridate)
print("Packages Loaded")
# Load the nottem dataset
data("nottem")
print("Nottem Loaded")
# Store the nottem dataset as a tibble
nottem_df <- as_tibble(nottem)
print("Nottem Stored as Tibble")
# Store the nottem dataset as a tidy df
nottem_tidy_df <- nottem_df %>%
mutate(year = year(date),
month = month(date)) %>%
select(date, year, month, temperature)
print("Nottem Stored as Tidy df")
# Average annual temperature by year df
average_temp_by_year_df <- nottem_tidy_df %>%
group_by(year) %>%
summarize(avg_temp = mean(temperature))
print("Average Annual Temp by Year Stored as df")
# Plot the annual temperature by year
ggplot(average_temp_by_year_df, aes(year, avg_temp)) +
geom_line() +
geom_smooth(method = "loess") +
ggtitle("Annual Temperature by Year") +
xlab("Year") +
ylab("Temperature (C)")+
ggsave("Annual_Temperature_by_Year.png")
print("AAT Plotted")
# Load the Titanic dataset
data("Titanic")
print("Titanic Loaded")
# Store the Titanic dataset as a tibble
titanic_tibble_df <- as_tibble(Titanic)
print("Titanic Dataset Sored as Tibble")
# Uncount the tibble Titanic dataset and make each of the 4 variables a factor
titanic_factors_df <- titanic_tibble_df %>%
mutate_at(c("Class", "Age", "Sex", "Survived"), as.factor)
print("Tibble Uncounted")
# Compute the proportion of people that survived
num_survived <- sum(titanic_factors_df$Survived == "Yes")
num_total <- nrow(titanic_factors_df)
prop_survived <- num_survived/num_total
print("Surviver Ratio Computed")
# Count the number of passengers in each class
class_count_df <- titanic_factors_df %>%
group_by(Class) %>%
summarize(count = n())
print("Passengers Counted by Class")
# Count the number of passengers who survived in each class
class_survived_df <- titanic_factors_df %>%
filter(Survived == "Yes") %>%
group_by(Class) %>%
summarize(survived_count = n())
print("Class Survivers Counted")
# Append the class totals to the survival totals df
class_totals_df <- class_count_df %>%
left_join(class_survived_df, by = "Class")
print("Df Appended")
# Compute the proportion of those that survived by class
class_totals_df$prop_survived <- class_totals_df$survived_count/class_totals_df$count
print("Class Surviver Ratio Computed")
# Plot the proportion of those that survived by class
ggplot(class_counts, aes(x = Class, y = prop_survived)) +
geom_bar(stat = "identity") +
scale_y_continuous(limits = c(0, 1), labels = scales::percent_format()) +
labs(x = "Class", y = "Proportion of Passengers Survived",
title = "Proportion of Passengers Survived by Class") +
ggsave("proportion_survived_by_class.png")
print("Class Survivers Plotted")
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started