I'm trying to build a function that counts the number of complete cases in .csv (data frames) files, that is, the number of rows with values (not "NA"). The function I wrote can read the file (s) specified in the function call and count the number of lines with complete cases, but I need to return this information as a data frame, with 2 columns, "id" for the identification of the (all are identified with numbers) and "Nobs" for the number of observations (the complete cases). In this function, my read loop is not able to store the count results in that date frame. The figure below has some examples of what the function should return.
The function is this:
completeF<-function(directory,id) {
#set file location
address<-paste(getwd(), directory, sep="/")
#Creates objects for later results keeping
Completev<-data.frame(matrix(0,1,ncol=2))
temp<-data.frame(matrix(0,1,ncol=2))
#colnames(Completev)<-c("id","Nobs")
#colnames(temp)<-c("id","Nobs")
#read files
files <- dir(directory)
for (i in id){
#read files
each.file<-read.csv(paste(address,files[i],sep="/"),h=TRUE)
#count complete cases: count number of lines with values for sulfate and nitrate
obs<-na.omit(each.file)
rowcount <- nrow(obs)
#keep results in temporary data frame, and then the final one
temp<-cbind(i,rowcount)
Completev<-rbind(Completev,temp)
}
colnames(Completev)<-c("id","Nobs")
return(Completev)
}
ButwhatIgetis
>completeF("specdata",1:5)
Error in match.names(clabs, names(xi)) :
names do not match previous names
Running Traceback, I understood that the error occurs here: Completev