forked from abigailhaddad/r_workshop_code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.r
83 lines (64 loc) · 2.14 KB
/
functions.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
library(dplyr)
library(ggplot2)
library(readr)
clean_data <- function(data) {
return(na.omit(data))
}
calculate_avg_solar <- function(data) {
data %>%
group_by(Month) %>%
summarize(Average_Solar_Radiation = mean(Solar.R, na.rm = TRUE))
}
calculate_correlations <- function(data) {
data %>%
group_by(Month) %>%
summarize(Correlation = ifelse(n() > 1, cor(Ozone, Solar.R, use = "complete.obs"), NA_real_)) %>%
pull(Correlation)
}
shape_assignment <- function(correlation_value) {
if (is.na(correlation_value)) {
return(16) # Return a default shape for NA values
} else if (correlation_value > 0.5) {
return(19)
} else if (correlation_value > 0) {
return(17)
} else {
return(15)
}
}
get_month_name <- function(month_number) {
month.abb[month_number]
}
generate_and_save_plot <- function(data_for_month, correlation_for_month) {
month_name <- get_month_name(unique(data_for_month$Month)[1])
shape_for_plot <- shape_assignment(correlation_for_month)
plot_for_month <- ggplot(data_for_month, aes(x = Solar.R, y = Ozone)) +
geom_point(shape = shape_for_plot) +
ggtitle(month_name)
print(plot_for_month)
ggsave(filename = paste0("plot_", tolower(month_name), ".png"), plot = plot_for_month)
}
plot_data_by_month <- function(data, correlations) {
unique_months <- unique(data$Month)
for(month in unique_months) {
data_for_month <- data %>% filter(Month == month)
correlation_for_month <- correlations[month]
generate_and_save_plot(data_for_month, correlation_for_month)
}
}
save_cleaned_data <- function(data) {
readr::write_csv(data, "cleaned_data.csv")
}
analyze_airquality_data <- function(input_data) {
cleaned_data <- clean_data(input_data)
avg_solar <- calculate_avg_solar(cleaned_data)
cat("\nAverage Solar Radiation for Each Month:\n")
print(avg_solar)
correlations <- calculate_correlations(cleaned_data)
cat("\nCorrelation between Ozone and Solar Radiation for Each Month:\n")
print(correlations)
plot_data_by_month(cleaned_data, correlations)
save_cleaned_data(cleaned_data)
}
# Snapshot the environment after all installations
renv::snapshot()