Skip to content

Instantly share code, notes, and snippets.

@Luckyeva
Last active February 9, 2016 04:14
Show Gist options
  • Save Luckyeva/7347b05bf297bec6d941 to your computer and use it in GitHub Desktop.
Save Luckyeva/7347b05bf297bec6d941 to your computer and use it in GitHub Desktop.
This is a function I wrote in R to summarise the number of complete cases in any given table
# R assignment 1 - Air Pollution
# preparation for R workspace
myTable <- NULL
mydata <- NULL
idd <- NULL
nobs <- NULL
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
## Return a data frame of the form
## id nobs
## 1 117
## 2 1041
## set working dir
setwd(paste("/Users/lucky1eva/Downloads/", directory, sep = '' ))
## transform the id into idd a vector of strings
for (i in 1:length(id)) {
if (id[i] < 10) {
idd[i] <- paste("00", id[i], sep = '')
} else if (id[i] >= 10 && id[i] < 100) {
idd[i] <- paste("0", id[i], sep = '')
} else {
idd[i] <- id[i]
}
}
## read CSV of selection and subset the table by complete cases function
for (j in 1:length(idd)) {
myTable <- read.csv(paste(idd[j], ".csv", sep = ''), header = TRUE)
cc <- complete.cases(myTable)
nobs[j] <- nrow(myTable[cc, ])
}
## return the resulted numbers in id and nobs
cbind(id, nobs)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment