forked from hqphat/coursera-r-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41984d6
commit 0ebcb84
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
## Write a function that reads a directory full of files and reports the number of completely observed cases in each data file. | ||
## The function should return a data frame where the first column is the name of the file and the second column is the number of complete cases. | ||
## A prototype of this function follows | ||
complete <- function(directory, id = 1:332) { | ||
|
||
## Get a list of filenames | ||
filenames <- list.files(path=directory, pattern="*.csv") | ||
|
||
ids <-vector() | ||
counts = vector() | ||
|
||
## Loop over the passed id's | ||
for(i in id) { | ||
## Pad the i to create a filename | ||
filename = sprintf("%03d.csv", i) | ||
filepath <- paste(directory, filename, sep="/") | ||
## Load the data | ||
data <- read.csv(filepath) | ||
## | ||
ids = c(ids, i) | ||
completeCases = data[complete.cases(data),] | ||
counts = c(counts, nrow(completeCases)) | ||
} | ||
|
||
dt <- data.frame(id=ids, nobs=counts) | ||
dt | ||
} |