forked from abigailhaddad/r_workshop_code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_code.R
53 lines (44 loc) · 1.52 KB
/
new_code.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Loading necessary libraries
library(ggplot2)
library(dplyr)
# Fetching data
data <- airquality
# Data Cleaning - Remove rows with NA
cleaned_data <- data %>%
na.omit()
# Function to calculate average solar radiation for each month
calculate_average_solar <- function(month, data) {
avg_solar <- data %>%
filter(Month == month) %>%
summarise(avg_solar = mean(Solar.R)) %>%
pull()
return(avg_solar)
}
# Function to calculate correlation between Ozone and Solar Radiation for each month
calculate_correlation <- function(month, data) {
correlation <- cor(data$Ozone[data$Month == month], data$Solar.R[data$Month == month])
return(correlation)
}
# Calculating average and print to console
months <- c(5, 6, 7, 8, 9)
for (month in months) {
avg_solar <- calculate_average_solar(month, cleaned_data)
print(paste("Average Solar Radiation for Month", month, ": ", avg_solar))
}
# Calculating correlation and print to console
for (month in months) {
correlation <- calculate_correlation(month, cleaned_data)
print(paste("Correlation for Month", month, ": ", correlation))
}
# Function to save plots
save_plots <- function(month, data) {
plot_data <- data[data$Month == month, ]
g <- ggplot(plot_data, aes(x = Solar.R, y = Ozone)) + geom_point(aes(shape = factor(Month))) + ggtitle(month.name(month))
ggsave(paste("plot_", tolower(month.name(month)), ".png", sep = ""), g)
}
# Save Plots
for (month in months) {
save_plots(month, cleaned_data)
}
# Save data
write.csv(cleaned_data, "cleaned_data.csv", row.names = FALSE)