here is my code:
# 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(date = floor_date(date, unit = "year"),
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")
here are the errors:
“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”
And finally, here are the paramiters I'm working from: In a blank R Script file inside of RStudio, write and execute lines that do the following:
Use comments to create a title area that includes your name and assignment name For each major bullet point below, write a header/comment that briefly describes what each line is doing. For every operation, make sure to print the results with print() Check if pacman is installed and install it if not Use pacman to load/install: pacman, datasets, tidyverse, tsibble, and lubridate Store the nottem dataset as a df using tsibble Store the nottem dataset as a tidy df with the date as an index and a separate column for just year, month, and temperature Create a df that shows the average annual temp by year Plot the annual temperature by year and add a smoothing line. (Appropriate Axis Labels and Title). Save the image. Store the Titanic dataset as a df Store the Titanic dataset as a df using tibble Uncount the tibble Titanic dataset and make each of the 4 variables a factor You can do 1 line at a time on variables changes or you can do similar ones in a group at once with mutate_at(c(“v1”,”v2”,”v3”,”v4”,…),var_type) Store result as a df Compute the proportion of people that survived. Num_Survived/Num_Total Create two variables: one for the total and one for how many survived summarise(n()) can be used to count the number of rows in a df Use filter to reduce the df to only those that survived Divide the counts Count and store in a df how many passengers were in each “Class” Group_by(Class) %>% then count Since the result is a column and not just a single data point like before, you should give a name to each of your counts: summarise(var_name=n()) Count and store in a df how many passengers survived in each “Class” Append the class totals to the survival totals df Should be a new df with the same but now 3 columns, Class, Survival Count, and Total Count Compute the proportion of those that survived by class and append as a 4th column in the survival totals df Can’t just divide the entire df to find the proportion (What is Crew/Crew?). Reference just a specific column. df$var Use ggplot to create a bar graph with Class on the x-axis and your proportion on the y-axis with proper axis labels and title. Scale the y axis in a way that assist with readability. Save the image. Use geom_bar(stat=”identity”) to make it work Save your Script file. Upload your script file and both images to complete the assignment.
I've asked friends, chegg, and chatGPT and can't seem to get any helpful advice.
I was told to change my mutate section to this:
# Store the nottem dataset as a tidy df
nottem_tidy_df <- nottem_df %>%
mutate(date = lubridate::floor_date(date, unit = "year"),
year = year(date),
month = month(date)) %>%
select(date, year, month, temperature)
print("Nottem Stored as Tidy df")
and this:
# Store the nottem dataset as a tidy df
nottem_tidy_df <- nottem_df %>%
mutate(date = stats::floor_date(date, unit = "year"),
year = year(date),
month = month(date)) %>%
select(date, year, month, temperature)
print("Nottem Stored as Tidy df")
but neither worked