AmarKahlon AmarKahlon - 2 months ago 5
R Question

Working Directories in R using for loop

The question I have is that I have a zip file which I am unpacking then I am using a for loop to go through the directories in the working directory and then change the working directory to the new folder that I have just unpacked. However, it doesn't seem to like it. Below are the two codes that I have used, one of them ends up with an error whereas the second one doesn't do what I want it to do. I think I am perhaps mixing some Python logic but if anyone could point me in the right direction that would be much appreciated. Thanks in advance ladies and gentlemen!

for (i in list.files("./")){
if (endsWith(tolower(i),"zip")){
unzip(i)
cat("Unzipped",i,"\n")
}
}

for (i in list.dirs(getwd())){
cat("This is i",i,"\n")
Root <- setwd(i)
cat("This is ROOT",Root,"\n")

}

print(Root)


The result I get from running the above code is as follows:

This is i F:/Testing with R
This is ROOT F:/Testing with R
This is i F:/Testing with R/ABC_Data
This is ROOT F:/Testing with R


As you can see, I would like it to loop through the folders and make ABC_Data the working directory so that I can then loop through the files but it doesn't like it.

The second code that I tried after going through some pages on Stackoverflow was amended using paste0() to below, which worked till a certain point but then gave an error:

> for (i in list.files("./")){
+ if (endsWith(tolower(i),"zip")){
+ unzip(i)
+ cat("Unzipped",i,"\n")
+ }
+ }
Unzipped GVA_BillData.zip
>
> for (i in list.dirs(getwd())){
+ cat("This is i",i,"\n")
+ Root <- paste0(path.expand(i),"/")
+ cat("This is ROOT",Root,"\n")
+
+ }
This is i F:/Testing with R
This is ROOT F:/Testing with R/
This is i F:/Testing with R/ABC_Data
This is ROOT F:/Testing with R/ABC_Data/
>
> print(Root)
[1] "F:/Testing with R/ABC_Data/"
>
> File_count <- 0
> for (a in list.files(Root)){
+ print(a)
+ if (endsWith(tolower(a),"csv")){
+ if (length(grep("service file",tolower(a)) > 0)){
+ Import <- read.csv(a, header = TRUE)
+ for (i in 1:nrow(Import)){
+ Import_Date <- Import[1,4]
+ if (File_count == 0){
+ write.table(c(Import[i,],Import_Date,a),"Output.csv",append = TRUE,sep = ",",row.names = FALSE,col.names = TRUE)
+ File_count <- File_count + 1
+ } else (write.table(c(Import[i,],Import_Date,a),"Output.csv",append = TRUE,sep = ",",row.names = FALSE,col.names = FALSE))
+ }
+ }
+ }
+ }
[1] "1234_126311.csv"
[1] "Service File.csv"
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'Service File.csv': No such file or directory
> print("Finished")
[1] "Finished"


So, as you can see in the second code it jumps to the folder but throws the error message. I have tried using simply path.expand(i) as well but that didn't work.

Any help on the above would be much appreciated.

Answer

I think the assignment when using setwd() is causing the issue. Adjusting you first code to this:

for (i in list.dirs(getwd())){
    cat("This is i",i,"\n")
    setwd(i)
    Root <- getwd()
    cat("This is ROOT",Root,"\n")    
}

seems to work for me.

Comments