I want to create a function that accepts a directory and then reads 100 csv files in that directory. I then want to store, in a data frame ("df" in my code) the file Id for each file and the number of rows that had no missing data. So my data frame will have two variables (columns) and 100 rows when done.
Here is my code. This works but the only row in my data frame, when it completes, is data on the last file processed, instead of all files. It appears my data frame is getting overwritten each time through the loop.
myFunction <- function(directory) {
df <-- data.frame(Id=integer(),nobs=integer())
for(i in 1:100) {
fileName <- sprintf("%03d.csv",i)
rowData <- read.csv(paste(directory,fileName,sep=""),header=T)
completeCases = rowData[complete.cases(rowData),]
df <- rbind(c(i,length(completeCases[[1]])))
}
df
}