以下函数读取一个充满文件的目录,并报告每个数据文件中完全观察到的案例数。该函数应返回一个数据框,其中第一列是文件的名称,第二列是完整案例的数量。该函数的原型遵循
complete <- function(directory,id = 1:332) {
##set up directory
file_names <-
dir(paste("C:/Users/Bruce/Desktop",directory,sep = "/"))
## combine the 332 files into data frame in the directory
data_frame <- do.call(rbind, lapply(file_names, read.csv))
## complete case
good <- complete.cases(data_frame)
rows <- data_frame[good,]
##structure of data_frame
str(rows)
nobs = vector(, 400)
for(i in id){
nobs <- nrow(rows[which(ID == 1),])
}
cbind(id, nobs)
}
complete("specdata")
错误
> complete("specdata")
'data.frame': 111802 obs. of 4 variables:
$ Date : Factor w/ 4018 levels "2003-01-01","2003-01-02",..: 279 285 291 297 303 315 321 327 333 339 ...
$ sulfate: num 7.21 5.99 4.68 3.47 2.42 1.43 2.76 3.41 1.3 3.15 ...
$ nitrate: num 0.651 0.428 1.04 0.363 0.507 0.474 0.425 0.964 0.491 0.669 ...
$ ID : int 1 1 1 1 1 1 1 1 1 1 ...
Show Traceback
Rerun with Debug
Error in `[.data.frame`(rows, ID == 1) : object 'ID' not found
结果应该是:
complete("specdata", c(2, 4, 8, 10, 12))
## id nobs
## 1 2 1041
## 2 4 474
## 3 8 192
## 4 10 148
## 5 12 96