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") while(all(log1$Rep!="useractivity_act")||all(log1$Rep!="useractivity_idle")||(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 files it is expecting. That could be because your working directory is not what 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 possible problem with k=k+1 later in the loop.
By the way, your code is horrible. You should indent and space around more things ( <- for example). And either use = for assignment or <-, don't use 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.sc...@auckland.ac.nz Graduate Officer, Department of Statistics Director of Consulting, Department of Statistics ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.