-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplete.R
More file actions
32 lines (27 loc) · 1.1 KB
/
complete.R
File metadata and controls
32 lines (27 loc) · 1.1 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
complete <- function(directory, id = 1:332) {
## 'directory' is a character vector of length 1 indicating
## the location of the CSV files
## 'id' is an integer vector indicating the monitor ID numbers
## to be used
## Returns a data frame of the form:
## id nobs
## 1 117
## 2 1041
## ...
## where 'id' is the monitor ID number and 'nobs' is the
## number of complete cases
##all files in the directory
filenames<-list.files(directory,pattern="*.csv", full.names=TRUE)
## create an empty column
nobs <- c();
for (index in id) {
## reads the files by the index
file_data <- read.csv(filenames[index], sep = ",");
## pulls all the rows that are complete, that doesnt have na. complete.cases function does that
complete_cases <- file_data[complete.cases(file_data),];
##nrow counts the number of rows in the matrix. then we put the latest entry below the previous nobs
nobs <- c(nobs, nrow(complete_cases));
}
##id comes in a vector e.g. 1,2,3.. so our nobs corresponds to the id and we bind them together
return(data.frame(cbind(id, nobs)));
}