Error in rbind: "Error in match.names (clabs, names (xi))"

5

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

asked by anonymous 24.08.2014 / 21:08

1 answer

2

You are reassigning the variable temp to each iteration and erasing the name of the columns.

One option is to copy the name of the columns at each iteration:

temp<-data.frame(i,rowcount) # nova data frame com seus próprios nomes
names(temp) <- names(Completev) 
Completev<-rbind(Completev,temp)

You can also create a new data frame already with the names for each interaction:

Completev<-rbind(Completev,data.frame(id=i,Nobs=rowcont)
However, in terms of performance this is not a good solution, it is much more efficient to pre-allocate a vector and construct the data.frame at a single time (see for some benchmarks ) .

    
24.08.2014 / 23:41