-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathjhu_prgwr_w2.Rmd
238 lines (188 loc) · 5.02 KB
/
jhu_prgwr_w2.Rmd
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
---
title: "JHU - Programming with R"
author: 郭耀仁
output:
revealjs::revealjs_presentation:
theme: black
highlight: zenburn
center: true
---
## 作業來源
[Coursera](https://www.coursera.org/) 的 [Programming with R](https://www.coursera.org/learn/r-programming) 課程第二週作業
## 作業目的
寫出三個函數與提供的資料集互動:
`pollutantmean(directory, pollutant, id = 1:332)`
`complete(directory, id = 1:332)`
`corr(directory, threshold = 0)`
## 資料集
[specdata.zip](https://storage.googleapis.com/jhu_coursera_data/specdata.zip)
## 資料集簡介
- 332 個 .csv 檔案
- 每個 .csv 檔案都有三個變數:
- Date
- sulfate
- nitrate
# 函數一
## 函數一結構
`pollutantmean(directory, pollutant, id = 1:332)`
計算不同觀測站特定污染物的平均數值
## 函數一提示
數列中若有遺漏值,記得在 `mean()` 函數中加入參數 `na.rm = TRUE`
```{r}
mean(c(1, 2, NA), na.rm = TRUE)
```
## 函數一範例
```{r echo=FALSE}
pollutantmean <- function(directory, pollutant, id = 1:332) {
csv_filenames <- c()
for (i in id) {
if (nchar(i) == 1) {
csv_filename <- paste0("00", i, ".csv")
csv_filenames <- c(csv_filenames, csv_filename)
} else if (nchar(i) == 2) {
csv_filename <- paste0("0", i, ".csv")
csv_filenames <- c(csv_filenames, csv_filename)
} else {
csv_filename <- paste0(i, ".csv")
csv_filenames <- c(csv_filenames, csv_filename)
}
}
csv_lst <- list()
for (i in 1:length(csv_filenames)) {
csv_files_dir <- paste0(directory, "/", csv_filenames[i])
csv_lst[[i]] <- read.csv(csv_files_dir, stringsAsFactors = FALSE)
}
df <- csv_lst[[1]]
if (length(csv_lst) != 1) {
for (i in 2:length(csv_lst)) {
df <- rbind(df, csv_lst[[i]])
}
}
filtered_vector <- df[, pollutant]
ans <- mean(filtered_vector, na.rm = TRUE)
return(ans)
}
```
```{r}
my_dir <- "~/Downloads/specdata"
pollutantmean(my_dir, "sulfate", 1:10)
pollutantmean(my_dir, "nitrate", 70:72)
pollutantmean(my_dir, "nitrate", 23)
```
# 函數二
## 函數二結構
`complete(directory, id = 1:332)`
計算不同 csv 檔案中的完整觀測值列數
## 函數二提示
使用 `complete.cases()` 來計算完整的觀測值個數。
```{r}
logi_vec <- complete.cases(cars)
n_completes <- sum(logi_vec)
n_completes
test_df <- cars
test_df[1, "dist"] <- NA
logi_vec <- complete.cases(test_df)
n_completes <- sum(logi_vec)
n_completes
```
## 函數二範例
```{r echo = FALSE}
complete <- function(directory, id = 1:332) {
csv_filenames <- c()
for (i in id) {
if (nchar(i) == 1) {
csv_filename <- paste0("00", i, ".csv")
csv_filenames <- c(csv_filenames, csv_filename)
} else if (nchar(i) == 2) {
csv_filename <- paste0("0", i, ".csv")
csv_filenames <- c(csv_filenames, csv_filename)
} else {
csv_filename <- paste0(i, ".csv")
csv_filenames <- c(csv_filenames, csv_filename)
}
}
csv_lst <- list()
for (i in 1:length(csv_filenames)) {
csv_files_dir <- paste0(directory, "/", csv_filenames[i])
csv_lst[[i]] <- read.csv(csv_files_dir, stringsAsFactors = FALSE)
}
df_id <- id
nobs <- c()
for (i in 1:length(csv_lst)) {
n_complete_cases <- sum(complete.cases(csv_lst[[i]]))
nobs <- c(nobs, n_complete_cases)
}
return_df <- data.frame(id = df_id, nobs = nobs)
return(return_df)
}
```
```{r}
my_dir <- "~/Downloads/specdata"
complete(my_dir, 1)
complete(my_dir, c(2, 4, 8, 10, 12))
```
## 函數二範例二
```{r}
complete(my_dir, 30:25)
complete(my_dir, 3)
```
# 函數三
## 函數三結構
`corr(directory, threshold = 0)`
計算完整觀測值個數大於等於 `threshold` 的 csv 檔案中污染物的相關係數
## 函數三提示
- 使用 `cor(x, y, use = "pairwise.complete.obs")` 函數計算相關係數
## 函數三範例
```{r echo = FALSE}
corr <- function(directory, threshold = 0) {
csv_filenames <- list.files(directory)
csv_directories <- paste0(directory, "/", csv_filenames)
csv_filelist <- list()
for (i in 1:length(csv_filenames)) {
csv_filelist[[i]] <- read.csv(csv_directories[i])
}
nobs <- c()
for (i in 1:length(csv_filelist)) {
n_complete_cases <- sum(complete.cases(csv_filelist[[i]]))
nobs <- c(nobs, n_complete_cases)
}
filter_vector <- nobs >= threshold
if (sum(filter_vector) == 0) {
cor_vector <- c()
return(cor_vector)
} else {
filtered_list <- csv_filelist[filter_vector]
cor_vector <- c()
for (i in 1:length(filtered_list)) {
cor_vector[i] <- cor(filtered_list[[i]]$sulfate, filtered_list[[i]]$nitrate, use = "pairwise.complete.obs")
}
cor_vector <- cor_vector[!is.na(cor_vector)]
return(cor_vector)
}
}
```
## 函數三範例二
```{r}
my_dir <- "~/Downloads/specdata"
cr <- corr(my_dir, 150)
head(cr)
summary(cr)
```
## 函數三範例三
```{r}
cr <- corr(my_dir, 400)
head(cr)
summary(cr)
```
## 函數三範例四
```{r}
cr <- corr(my_dir, 5000)
summary(cr)
length(cr)
```
## 函數三範例五
```{r}
cr <- corr(my_dir)
summary(cr)
length(cr)
```