[R] Import the files.

Rowe, Brian Lee Yung (Portfolio Analytics) B_Rowe at ml.com
Thu Mar 5 22:41:12 CET 2009

I get the same error message when I do this:

> read.table('NA',header=TRUE,stringsAsFactors=FALSE)
Error in file(file, "r") : unable to open connection
In addition: Warning message:
In file(file, "r") :
  cannot open file 'NA', reason 'No such file or directory'

I get this error when I call list.files (the way you do):

> name <- list.files(path=".", pattern=NULL, all.files=FALSE,
full.names=FALSE, recursive=FALSE, ignore.case=FALSE)
Error in list.files(path = ",", pattern = NULL, all.files = FALSE,
full.names = FALSE,  :
  unused argument(s) (ignore.case = FALSE)

> name[1]
[1] NA

So you should probably fix your call to list.files and then name will be
properly populated.

> name <- list.files(path=".", pattern=NULL, all.files=FALSE,
full.names=FALSE, recursive=FALSE)
> name[1]
[1] "foo.txt"


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of David Scott
Sent: Thursday, March 05, 2009 3:57 PM
To: ling ling
Cc: r-help at r-project.org
Subject: Re: [R] Import the files.

On Thu, 5 Mar 2009, ling ling wrote:

> Dear all,
> I am a newcomer to R programming, I met the problem:
> I have a lot of .txt files in my directory.
> Firstly, I check whether the file satisfies the conditions:
> 1.empty
> 2.the "Rep" column of the file has no "useractivity_idle" or
> "useractivity_act"
> 3.even The "rep" has both of them, numbers of
"useractivity_idle"==numbers of "useractivity_act"==1
> If the file has one of those conditions, skip this file, jump to and
read the next .txt file:
> I made the programming as:
> name<-list.files(path = ".", pattern = NULL, all.files = FALSE,
>           full.names = FALSE, recursive = FALSE,
>           ignore.case = FALSE)
> for(k in 1:length(name)){
> log1<-read.table(name[k],header=TRUE,stringsAsFactors=FALSE)
> x<-which(log1$Rep=="useractivity_act")
> y<-which(log1$Rep=="useractivity_idle")
e")||(length(x)==1 && length(y)==1)||(file.info(name[k])$size== 0)){
> k=k+1
> log1<-read.table(name[k],header=TRUE,stringsAsFactors=FALSE)
> }
> ........
> }
> But I always get the following information:
> Error in file(file, "r") : cannot open the connection
> In addition: Warning message:
> In file(file, "r") : cannot open file 'NA': No such file or directory
> I have been exploring this for long time, any help would be
appreciated. Thanks a lot!
> Tammy

This is a bit of a guess but the message is saying it can't find the
it is expecting. That could be because your working directory is not
you think it is. Alternatively you have some name in your file list that

is not in the directory you are looking at.

To debug, start by issuing the name <- statement. Check what it is in 
names? Is it what you think it should be?

Then do k <- 1 and start running through the commands *inside* your for 
loop. Check at each point that you have what you expect to have in log1,

x, y etc.

Also check the for loop with k <- length(name). That looks like a
problem with k=k+1 later in the loop.

By the way, your code is horrible. You should indent and space around
things ( <- for example). And either use = for assignment or <-, don't
both in the same bit of code.

David Scott

David Scott	Department of Statistics
 		The University of Auckland, PB 92019
 		Auckland 1142,    NEW ZEALAND
Phone: +64 9 373 7599 ext 85055		Fax: +64 9 373 7018
Email:	d.scott at auckland.ac.nz

Graduate Officer, Department of Statistics
Director of Consulting, Department of Statistics

R-help at r-project.org mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.

This message w/attachments (message) may be privileged, confidential or proprietary, and if you are not an intended recipient, please notify the sender, do not use or share it and delete it. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Merrill Lynch. Subject to applicable law, Merrill Lynch may monitor, review and retain e-communications (EC) traveling through its networks/systems. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or error-free. References to "Merrill Lynch" are references to any company in the Merrill Lynch & Co., Inc. group of companies, which are wholly-owned by Bank of America Corporation. Securities and Insurance Products: * Are Not FDIC Insured * Are Not Bank Guaranteed * May Lose Value * Are Not a Bank Deposit * Are Not a Condition to Any Banking Service or Activity * Are Not Insured by Any Federal Government Agency. Attachments that are part of this E-communication may have additional important disclosures and disclaimers, which you should read. This message is subject to terms available at the following link: http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch you consent to the foregoing.

More information about the R-help mailing list