-
Notifications
You must be signed in to change notification settings - Fork 0
/
week2final.R
105 lines (83 loc) · 2.69 KB
/
week2final.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
zipfiels <- list.files("./specdata", full.names = TRUE)
df <- data.frame()
for (i in zipfiels) {
df <- rbind(df, read.csv(i, colClasses = c(Date="Date")))
}
fixnumber <- function(k)
{
ls <- character()
for (i in seq(length(k)))
{
if (k[i] < 10) {
ls[i] <- paste("00", k[i], sep="")
}
else if(k[i] < 100) {
ls[i] <- paste("0", k[i], sep="")
}
else
ls[i] <- paste(k[i])
}
ls <- na.omit(ls)
}
pollutantmean <- function(directory, polutant, id){
#declaring empty data frame which we would fill based on the id
df <- data.frame()
#getting all files in the directory
id <- fixnumber(id)
for (i in id) {
#adding all the csvs to a dataframe object
df <- rbind(df, read.csv(paste(getwd(), "/", directory,"/", i, ".csv", sep=""), colClasses = c(Date="Date")))
}
polid <- numeric()
if (polutant == "sulfate") {
polid <- 2
} else if (polutant == "nitrate"){
polid <- 3
}
else
print("Please select for a polutant either Sulfate or Nitrate")
mean(df[,polid], na.rm=TRUE)
}
complete <- function(directory, id = 1:332)
{
fin_datafr <- data.frame(filename=numeric(0), obs=numeric(0))
id <- fixnumber(id)
for (i in id) {
temp_df <- read.csv(paste(getwd(), "/", directory,"/", i, ".csv", sep=""), colClasses = c(Date="Date"))
temp_val <- temp_df[!is.na(temp_df$sulfate) & !is.na(temp_df$nitrate), ]
temp_val <- nrow(temp_val)
fin_datafr <- rbind(fin_datafr, list(filename=as(i,"numeric"), obs=temp_val))
}
fin_datafr
}
makeVector <- function(x, ...)
{
m <- NULL
set <- function(k)
{
x <<- k
m <<- NULL
}
get <- function() x
setmean <- function(me) m <<- me
getmean <- function() m
list(set = set, get = get, setmean = setmean, getmean = getmean)
}
test <- function(x)
{
if (x == "heart attack") {
return(11)
}
else if (x == "sok") {
result <- 12
}
else {
result <- 13
}
return(result)
}
pm <- proc.time()
for(n in 1:1000) {
sapply(split(DT$pwgtp15,DT$SEX),mean)
}
proc.time() - pm