对于循环复杂性......不了解错误 [英] For loop complexity… not understanding what is wrong

查看:901
本文介绍了对于循环复杂性......不了解错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过课程R程序课程。我正在做的分配问题如下:



编写一个函数,读取一个充满文件的目录,并报告每个数据文件中完全观察到的案例数。该函数应返回一个数据框,其中第一列是文件的名称,第二列是完整案例的数量。



此问题的数据集: https://d396qusza40orc.cloudfront.net/rprog%2Fdata%2Fspecdata.zip



我得到的错误是......

< pre lang =text> [< - 。data.frame(* tmp *,i,value = list)中的错误(日期= 1:2191,硫酸盐= c(NA,:新列将在现有列之后留下空洞





我的功能代码在这里:



完成< -function(目录,id =  1  332 ){
file_names< -list.files( specdata,full.names = TRUE)
dat< ; -data.frame()
comp_dat< -c()
inc< -0
for (i > in id){
dat [i]< - read.csv(file_names [i])
comp_dat [i]< -nrow(complete.cases(dat [i]))
inc< -inc + 1
}
show_result< -data.frame(inc,comp_dat)
head(show_result)
}

解决方案

I建议使用调试器。

一步一步地运行程序,你会看到错误在哪里。

你将能够检查变量。


I'm going through coursera R progrmming course. the assignment question im doing is as follows:

Write a function that reads a directory full of files and reports the number of completely observed cases in each data file. The function should return a data frame where the first column is the name of the file and the second column is the number of complete cases.

The dataset for this question: https://d396qusza40orc.cloudfront.net/rprog%2Fdata%2Fspecdata.zip

the error im getting is...

Error in [<-.data.frame(*tmp*, i, value = list(Date = 1:2191, sulfate = c(NA, : new columns would leave holes after existing columns



The code to my function is here:

complete<-function(directory, id=1:332){
  file_names<-list.files("specdata", full.names=TRUE)
  dat<-data.frame()
  comp_dat<-c()
  inc<-0
  for(i in id){
    dat[i]<-read.csv(file_names[i])
    comp_dat[i]<-nrow(complete.cases(dat[i]))
    inc<-inc+1
  }
    show_result<-data.frame(inc,comp_dat)
    head(show_result)
}

解决方案

I recommend to use the debugger.
run the program step by step, you will see where is the error.
You will be able to inspect variables.


这篇关于对于循环复杂性......不了解错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆