Skip to content

Commit

Permalink
Homework 1 problem 2
Browse files Browse the repository at this point in the history
  • Loading branch information
edj-boston committed Apr 20, 2014
1 parent 41984d6 commit 0ebcb84
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions programming-assignment-1/complete.R
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
}

0 comments on commit 0ebcb84

Please sign in to comment.