forked from abigailhaddad/r_workshop_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPTCode.R
More file actions
66 lines (52 loc) · 1.61 KB
/
GPTCode.R
File metadata and controls
66 lines (52 loc) · 1.61 KB
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
library(ggplot2)
library(tidyverse)
# Function to calculate monthly averages
calculateMonthlyAverages <- function(data) {
monthlyAverages <- data %>%
group_by(Month) %>%
summarize(
avgSolar = mean(Solar.R, na.rm = TRUE)
)
return(monthlyAverages)
}
# Function to calculate monthly correlations
calculateMonthlyCorrelations <- function(data) {
monthlyCorrelations <- data %>%
group_by(Month) %>%
summarize(
correlation = cor(Ozone, Solar.R, use = "complete.obs")
)
return(monthlyCorrelations)
}
# Function to save plots as objects and image files
savePlots <- function(data, filePrefix) {
uniqueMonths <- unique(data$Month)
plots <- list()
for (month in uniqueMonths) {
subsetData <- data[data$Month == month, ]
if (nrow(subsetData) > 0) {
g <- ggplot(subsetData, aes(x = Solar.R, y = Ozone)) +
geom_point(aes(shape = factor(Month))) +
ggtitle(month.name[month])
# Save plot as an image file
ggsave(paste0(filePrefix, tolower(month.name[month]), ".png"), g)
# Save plot as an object in the list with the month name as the key
plots[[tolower(month.name[month])]] <- g
}
}
return(plots)
}
# Loading data
data <- airquality
# Data Cleaning - Remove rows with NA
data <- na.omit(data)
# Calculate monthly averages
averages <- calculateMonthlyAverages(data)
print(averages)
# Calculate monthly correlations
correlations <- calculateMonthlyCorrelations(data)
print(correlations)
# Visualization and save plots
savedPlots <- savePlots(data, "plot_")
# Save data
write.csv(data, "cleanedData.csv", row.names = FALSE)