Despite reading the existing answers about this Error, I still don't know how to fix this problem in my particular case.
I have to get the sum of complete cases in a list of files. Each file (e.g. file1 corresponds to an id (e.g id1 for file1). My goal is to get a data frame with the number of complete cases for each id (therefore for each file, as file1 contains the pollutants of id1, and file2 contains the pollutants of id2 and so on)
When I run the function: complete("pollu", 1:10)
--> everything works perfectly
But when I run the functions:
complete("pollu", 34)
I get ID 34 times, with 33 times returning NA and finally returning the number of complete cases.
complete(".", c(2, 4, 8, 10, 12))
I get the error:
Error in data.frame(id, nobs) : arguments imply differing number of rows: 5, 12
Any help on understanding the error and fixing it would be appreciated.
complete <- function(directory,id=1:332) {
nobs <- vector()
files <- list.files(directory)
for (i in id) {
ID <- id
file <- read.csv(files[i])
nobs[i] <- sum(complete.cases(file),na.rm = TRUE)
}
df <- data.frame(ID,nobs)
colnames(df) <- c("ID", "nobs")
return (df)
}