-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpollutantmean.R
42 lines (29 loc) · 1.1 KB
/
pollutantmean.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
pollutantmean <- function(directory, pollutant, id = 1:332) {
#Selects .csv's that are going to be read according to "<ID_Number>.csv".
cleandata <- 0;
for(i in id) {
df <- readDirectory(nameDirectory(directory)) #Gets directories and reads data.
clean <- RemoveNa(df) #Removes NA values.
cleandata <- cleandata + clean #Stores clean data.
}
mean(cleandata) #Mean of clean data
}
readDirectory(directory) {
df <- read.csv(directory, header=TRUE)
df
}
nameDirectory(directory) {
if(i < 10) {
directory <- paste(directory, "00", as.character(i),".csv", sep = "")
} else if(i >= 10 && i < 100) {
directory <- paste(directory,"0", as.character(i),".csv", sep = "")
} else if(i>=100) {
directory <- paste(directory, as.character(i),".csv", sep = "")
}
directory
}
RemoveNA(df) {
bad <-is.na(df$pollutant)
clean <- df$pollutant[!bad]
clean
}