Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Error in file(file, "rt") : cannot open the connection - How to solve it?
Next
In Process
mwillms
  • 30
mwillms
Asked: May 4, 20222022-05-04T11:12:47+00:00 2022-05-04T11:12:47+00:00In: r

Error in file(file, “rt”) : cannot open the connection – How to solve it?

  • 30

. Advertisement .

..3..

. Advertisement .

..4..

 I get the error: Error in file(file, “rt”) : cannot open the connection when I try to run the program below:

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 appears the system notifies as follows:

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

I tried a few ways, but it didn’t work. Can someone suggest a working solution to solve this problem? Thanks!

"rt")cannot open the connectionerror in file(file
  • 4 4 Answers
  • 141 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

4 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. gudrunmedhurst
    2022-05-09T07:07:38+00:00Added an answer on May 9, 2022 at 7:07 am

    Another option is to open the csv file by providing the full path of the file and not accessing the directory in question:

    # Read the CSV file by giving the complete file path
    dataframe <- read.csv('C:\\Users\\harsh\\Desktop\\ITtutoria\\sample.csv',
    header=TRUE, stringsAsFactors=FALSE)
    
    # Display the dataframe
    dataframe
    • 15
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. shagenes
    2022-05-04T14:41:28+00:00Added an answer on May 4, 2022 at 2:41 pm

    You need to change directory, from “./datatest” changed to “./datatest/”

    if(grep("datatest",directory) ==1)
    {
    
        directory <- ("./datatest/")
    }
    • 12
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  3. fvolkman
    2022-05-09T07:14:44+00:00Added an answer on May 9, 2022 at 7:14 am

    For your question, I just faced the same error and corrected by removing spacing in address using paste0 instead of paste

    filepath=paste0(directory,"/",filename[1],sep="")
    • 12
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  4. plesch
    2022-05-04T14:53:08+00:00Added an answer on May 4, 2022 at 2:53 pm
    Warning message:
    In file(file, "rt") : cannot open file './datatest001.csv': No such file or directory

    Cause of the warning message : Maybe windows generated a name of ./datatest001.csv.csv not ./datatest001.csv

    • 11
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.