我有下面的代码,我试图为每次迭代添加一个观察到nobs。我尝试过多种方法:预定义nobs向量,将结果预定义为数据帧,将c(nobs,nobs2)更改为data.frame(nobs,nobs2)。
我收到的内容:
Warning messages:
1: In cbind(id, nobs) :
number of rows of result is not a multiple of vector length (arg 2)
2: In cbind(id, nobs) :
number of rows of result is not a multiple of vector length (arg 1)
如果我不调用该函数,并单独执行每一行,我会收到以下结果:
id nobs
[1,] 1 ?
[2,] 2 117
[3,] 3 1041
[4,] 1 243
我期待输出:
id nobs
1 243
2 1041
3 117
代码:
complete <- function(directory, id = 1:3) {
files_list <- list.files(directory, full.names=TRUE)
dat <- data.frame()
for (i in id) {
dat <- data.frame(read.csv(files_list[i]))
dat_cln<- dat[complete.cases(dat), ]
nobs2 <- nrow(dat_cln)
nobs <- c(nobs, nobs2)
results <- cbind(id, nobs)
}
}