-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcomplete.R
More file actions
47 lines (33 loc) · 1.59 KB
/
complete.R
File metadata and controls
47 lines (33 loc) · 1.59 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
## Return 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
## Pad the ID number to be a .csv file in the correct format
csvId <- paste(formatC(id, width = 3, flag = 0), "csv", sep = ".")
## Set the directory and the complete path for loading .csv files
completePath <- paste(directory, csvId, sep = "/")
## Initialize an empty vector for the number of observations
nobs <- c()
## Start a for loop if there are multiple .csv files to be loaded
for (i in seq.int(completePath)) {
## Reads in the .csv files one by one
input <- read.csv(completePath[i])
## Creates a dataframe with only complete cases
good <- na.omit(input)
## Counts the number of complete cases
tempNobs <- nrow(good)
## Adds the number of complete cases to the already existing vector
nobs <- c(nobs,tempNobs)
}
## Creates a dataframe of id numbers and numbers of complete cases for each .csv file
result <- data.frame(id, nobs)
return (result)
}