Created
October 7, 2013 18:25
-
-
Save timmyshen/6872633 to your computer and use it in GitHub Desktop.
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.
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
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 | |
source(file='getmonitor.R') | |
output <- data.frame() | |
for ( i in id) { | |
# | |
oneMonitorData <- getmonitor(id=i, directory=directory) | |
# print(head(oneMonitorData)) | |
one.nobs <- nrow(oneMonitorData[complete.cases(oneMonitorData),]) | |
id.num <- as.integer(x=i) | |
one.row <- c(id.num, one.nobs) | |
# print(one.row) | |
output <- rbind(output, one.row) | |
} | |
# print(one.row) | |
# print(as.data.frame(one.row)) | |
colnames(output) <- c('id', 'nobs') | |
output | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just wanted to say thank you so much for posting these! They've been saving my life taking this course, it really helps to try and figure out what went wrong with my code and how to try and go about fixing it.