-
Notifications
You must be signed in to change notification settings - Fork 10
/
proj_prgwr.Rmd
221 lines (175 loc) · 4.94 KB
/
proj_prgwr.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
---
title: "Project: Programming with R"
author: "Tony Yao-Jen Kuo"
output:
revealjs::revealjs_presentation:
highlight: pygments
reveal_options:
slideNumber: true
previewLinks: true
---
```{r include=FALSE}
knitr::opts_chunk$set(results = "hold", message = FALSE)
```
# Project Overview
## Project source
Assignment from [Programming with R](https://www.coursera.org/learn/r-programming)
## Write 3 functions to interact with data
>- `pollutantmean(directory, pollutant, id = 1:332)`
>- `complete(directory, id = 1:332)`
>- `corr(directory, threshold = 0)`
## Getting data
[specdata.zip](https://storage.googleapis.com/jhu_coursera_data/specdata.zip)
## How to download, unzip data with R?
- `download.file()` for downloading
- `unzip()` for unzipping
## About data
- 332 CSV files after unzipping
- Each CSV file has 4 variables
# Function 1
## Try to calculate the mean value of certain pollutant from different stations
`pollutantmean(directory, pollutant, id = 1:332)`
## Hints for function 1
- Set `na.rm = TRUE` in `mean()` if there are NAs
## Sample outputs
```{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 <- "/Users/kuoyaojen/Downloads/specdata"
pollutantmean(my_dir, "sulfate", 1:10)
pollutantmean(my_dir, "nitrate", 70:72)
pollutantmean(my_dir, "nitrate", 23)
```
# Function 2
## Try to calculate how many complete rows are in different CSV files
`complete(directory, id = 1:332)`
## Hints for function 2
- Use `complete.cases()` to get complete rows from a data frame
## Sample output 1
```{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 <- "/Users/kuoyaojen/Downloads/specdata"
complete(my_dir, 1)
complete(my_dir, c(2, 4, 8, 10, 12))
```
## Sample output 2
```{r}
complete(my_dir, 30:25)
complete(my_dir, 3)
```
# Function 3
## Try to calculate the correlation coefficient for CSV files, which have complete observations over `threshold`
`corr(directory, threshold = 0)`
## Hints for function 3
- Use `cor(x, y, use = "pairwise.complete.obs")` function for correlation coefficient
```{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)
}
}
```
## Sample output 1
```{r}
my_dir <- "/Users/kuoyaojen/Downloads/specdata"
cr <- corr(my_dir, 150)
head(cr)
summary(cr)
```
## Sample output 2
```{r}
cr <- corr(my_dir, 400)
head(cr)
summary(cr)
```
## Sample output 3
```{r}
cr <- corr(my_dir, 5000)
summary(cr)
length(cr)
```
## Sample output 4
```{r}
cr <- corr(my_dir)
summary(cr)
length(cr)
```