简体   繁体   English

文件错误(文件,“rt”):complete.cases 程序中的“描述”参数无效

[英]Error in file(file, "rt") : invalid 'description' argument in complete.cases program

I am writing an R function that reads a directory full of files and reports the number of completely observed cases in each data file.我正在编写一个 R function 读取一个充满文件的目录并报告每个数据文件中完全观察到的案例的数量。 The function returns a data frame where the first column is the name of the file and the second column is the number of complete cases. function 返回一个数据框,其中第一列是文件名,第二列是完整案例的数量。

such as,如,

id nobs
1  108
2  345
...
etc

Here is the function I wrote:这是我写的 function:

complete <- function(directory, id = 1:332) {

  for(i in 1:332) {
    path<-paste(directory,"/",id,".csv",sep="")
    mydata<-read.csv(path)
    #nobs<-nrow(na.omit(mydata))
    nobs<-sum(complete.cases(mydata))
    i<-i+1
  }

  completedata<-c(id,nobs)
}

I execute the function:我执行 function:

complete("specdata",id=1:332)

but I'm getting this error:但我收到此错误:

Error in file(file, "rt") : invalid 'description' argument

I also tried the traceback() function to debug my code and it gives this output:我还尝试了traceback() function 来调试我的代码,它给出了这个 output:

traceback()
# 4: file(file, "rt") at #6
# 3: read.table(file = file, header = header, sep = sep, quote = quote, 
#    dec = dec, fill = fill, comment.char = comment.char, ...) at #6
# 2: read.csv(path) at #6
# 1: complete("specdata", id = 1:332)

It's hard to tell without a completely reproducible example, but I suspect your problem is this line:没有一个完全可重现的例子很难说,但我怀疑你的问题是这一行:

path<-paste(directory,"/",id,".csv",sep="")

id here is a vector, so path becomes a vector of character strings, and when you call read.csv you're passing it all the paths at once instead of just one.这里的id是一个向量,所以 path 变成了一个字符串向量,当你调用read.csv你将一次传递所有路径而不是一个。 Try changing the above line to尝试将上面的行更改为

path<-paste(directory,"/",id[i],".csv",sep="")

and see if that works.看看这是否有效。

It seems you have a problem with your file path.您的文件路径似乎有问题。 You are passing the full vector id =c(1:332) to the file path name.您将完整的向量 id =c(1:332) 传递给文件路径名。 If your files are named 1.csv, 2.csv, 3.csv, etc..如果您的文件名为 1.csv、2.csv、3.csv 等。
You can change this line:您可以更改此行:

path<-paste(directory,"/",id,".csv",sep="")

to

path<-paste(directory,"/",i,".csv",sep="")

and leave out or rework the id input of your function.并省略或返工函数的 id 输入。

Instead of using a for to read the data in, you can try sapply .您可以尝试使用sapply ,而不是使用for来读取数据。 For example例如

mydata <- sapply(path, read.csv) . mydata <- sapply(path, read.csv)

Since path is a vector, sapply will iterate the vector and apply read.csv to it.由于path是一个向量, sapply将迭代该向量并将read.csv应用于它。 Therefore, there will be no need for the for loop and your code will be much cleaner.因此,将不需要的for循环和你的代码将是干净多了。

From there you will have a matrix which each of your files and their respective information from which you can extract the observations.从那里您将拥有一个matrix ,其中包含您的每个文件及其各自的信息,您可以从中提取观察结果。

To find the observations, you can do mydata[2,1][[1]] .要找到观察结果,您可以执行mydata[2,1][[1]] Remember that the rows will be your factors and your columns will be your files.请记住,行将是您的因素,而您的列将是您的文件。

I am working on the exact problem.. file names in the directory "specdata" are named with 001.csv and 002.csv.... 099.csv all the way to file 332.csv however, when you are recalling id=1 then your file name becomes 1.csv which does not exist in the directory.我正在解决确切的问题.. 目录“specdata”中的文件名以 001.csv 和 002.csv 命名.... 099.csv 一直到文件 332.csv 但是,当您回忆 id= 1 那么你的文件名变成 1.csv 目录中不存在的。 try using this function to get the path of each id file.尝试使用此函数获取每个 id 文件的路径。

filepaths <- function (id){
    allfiles = list.files(getwd())
    file.path(getwd(), allfiles[id])
}

I met the same problem in this sentence:我在这句话中遇到了同样的问题:

Browse[2]> read.csv(list.files(".", "XCMS-annotated-diffreport--.*csv$"), row.names = 1)
Error in file(file, "rt") : invalid 'description' argument

then, I found there are two different csv files in the same path, like this:然后,我发现同一路径中有两个不同的 csv 文件,如下所示:

Browse[2]> list.files(".", "XCMS-annotated-diffreport--.*csv$")
[1] "XCMS-annotated-diffreport--1-vs-2-Y.csv" "XCMS-annotated-diffreport--1-vs-2.csv"  

When I deleted one file, it works again.当我删除一个文件时,它又可以工作了。

In my code the problem was that I´ve misstyped the name of the file.在我的代码中,问题是我输错了文件名。 And the other file was´t in this directory.另一个文件不在这个目录中。 So check if all files are where they should be.因此,检查所有文件是否都在它们应该在的位置。

I had this problem because I was trying to run a for loop against the data frame and not a vector:我遇到这个问题是因为我试图对数据框而不是向量运行 for 循环:

  ids <- th[th$nobs > threshold,]
  for(i in ids) {

this is what the variable "ids" looks like:这就是变量“ids”的样子:

     id nobs
2     2 1041
154 154 1095
248 248 1005

should have been:本来应该:

  ids <- th[th$nobs > threshold,]
  for(i in ids$id) {

将对象 id 更改为 i - 因为您在循环中使用迭代对象 i ie path<-paste(directory,"/",id,".csv",sep="") 到 ie path<-paste(directory," /",i,".csv",sep="")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R闪亮:文件错误(文件,“ rt”):无效的“描述”参数 - R shiny: Error in file(file, “rt”) : invalid 'description' argument 文件(文件,“rt”)中的错误:调用函数时“描述”参数无效 - Error in file(file, "rt") : invalid 'description' argument , when calling the function complete.cases 的问题:参数的“类型”(列表)无效 - Issue with complete.cases: invalid 'type' (list) of argument 文件夹内每个文件中的complete.cases值,其表为[id],[complete.cases value]列 - Value of complete.cases in each file inside a folder as a table with columns [id],[complete.cases value] for(seq_along(data_file)中的i:file(file,“ rt”)中的错误:无效的&#39;description&#39;参数 - for(i in seq_along(data_file): Error in file(file, “rt”) : invalid 'description' argument 文件错误(文件,“rt”):读取 csv 文件时“描述”参数无效 - Error in file(file, "rt") : invalid 'description' argument when reading csv files gzfile(fname,open =“ rt”)中的错误:无效的&#39;description&#39;参数 - Error in gzfile(fname, open = “rt”) : invalid 'description' argument 文件错误(描述 = xlsxFile):使用 lapply 时“描述”参数无效 - Error in file(description = xlsxFile) : invalid 'description' argument when using lapply “结果必须有长度......”&#39;complete.cases&#39;错误 - "Result must have length..." error with 'complete.cases' 文件中的错误(文件,ifelse(追加,“a”,“w”)):无效的“描述”参数 - Error in file(file, ifelse(append, “a”, “w”)) : invalid 'description' argument
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM