文件(文件,“rt”)中的错误:无法打开连接 [英] Error in file(file, "rt") : cannot open the connection

查看:11978
本文介绍了文件(文件,“rt”)中的错误:无法打开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R的新手,在广泛研究这个错误后,我还是找不到解决方案。这里是代码。我检查了我的工作目录,并确保文件在正确的目录。欣赏它。感谢

  pollutantmean<  -  function(directory,pollutant =nitrate,id = 1:332)
{ if(grep(specdata,directory)== 1)
{
目录< - (./specdata)
}
mean_polldata&
specdatafiles< - as.character(list.files(directory))
specdatapaths< - paste(directory,specdatafiles,sep =)
for(i in id)
{
curr_file< - read.csv(specdatapaths [i],header = T,sep =,)
head(curr_file)
污染物
remove_na< - curr_file [!is.na(curr_file [,pollutant]),pollutant]
mean_polldata < - c(mean_polldata,remove_na)
}
{
mean_results& (mean_polldata)
return(round(mean_results,3))
}
}

我得到的错误如下:

 文件中出现错误打开连接

文件(文件,rt)

read.table(file = file,header = header,sep = sep,quote = quote,
dec = dec,fill = fill,comment.char = comment.char,...)

read.csv(specdatapaths [i],header = T,sep =,)

pollutantmean3(specdata,sulfate,1:10)

此外:警告消息:
在文件(文件,rt)中:
无法打开文件'./specdata001.csv':没有这样的文件或目录


解决方案

将工作目录设置为一级/文件夹更高。例如,如果已经设置为:

  setwd(C:/ Users / Z / Desktop / Files / RStudio / Coursera / specdata)

返回上一级并将其设置为:

  setwd(C:/ Users / Z / Desktop / Files / RStudio / Coursera)
pre>

换句话说,不要将specdata文件夹作为您的工作目录。


I'm new to R, and after researching this error extensively, I'm still not able to find a solution for it. Here's the code. I've checked my working directory, and made sure the files are in the right directory. Appreciate it. Thanks

pollutantmean <- function(directory, pollutant = "nitrate", id= 1:332)            
{                 if(grep("specdata",directory) ==1) 
            {
                    directory <- ("./specdata")
            }
            mean_polldata <- c()
            specdatafiles <- as.character(list.files(directory))
            specdatapaths <- paste(directory, specdatafiles, sep="")
                            for(i in id) 
                    {
                    curr_file <- read.csv(specdatapaths[i], header=T, sep=",")
                    head(curr_file)
                    pollutant
                    remove_na <- curr_file[!is.na(curr_file[, pollutant]), pollutant]
                    mean_polldata <- c(mean_polldata, remove_na)
                    }
            {
                    mean_results <- mean(mean_polldata)
                    return(round(mean_results, 3))
            }
} 

The error I'm getting is below:

Error in file(file, "rt") : cannot open the connection

file(file, "rt")

read.table(file = file, header = header, sep = sep, quote = quote, 
    dec = dec, fill = fill, comment.char = comment.char, ...)

read.csv(specdatapaths[i], header = T, sep = ",")

pollutantmean3("specdata", "sulfate", 1:10)

In addition: Warning message:
In file(file, "rt") :
  cannot open file './specdata001.csv': No such file or directory

解决方案

Set your working directory one level/folder higher. For example, if it is already set as:

setwd("C:/Users/Z/Desktop/Files/RStudio/Coursera/specdata")

go up one level back and set it as:

setwd("C:/Users/Z/Desktop/Files/RStudio/Coursera")

In other words, do not make "specdata" folder as your working directory.

这篇关于文件(文件,“rt”)中的错误:无法打开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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