Do loop for file entry

I need to have this code placed in a do loop so that I can read in n test1.nm7 not just the one represented in the listing. How can I do this?
getwd()
setwd("C:/biphenpeds_nda205831/physiolpopfit/bootstrap/test1.nm7/")
datr<-read.table(file="param.tbl",header=T)
#fix(datr)
fix(datr)
write.csv (datr, file="fitsubj.csv")
reprex()

Hello,

I'm afraid your request is not very clear and the code you provided is not a reprex we can work with. Could you try and generate a more general example of what you like to accomplish and clearly show the input and output you're expecting.

For instance, I don't see where the loop is coming in, and the fix command is something that requires manual entry thus cannot be automated per definition.

Please use the reprex guide if needed. A reprex consists of the minimal code and data needed to recreate the issue you're having. You can find instructions how to build and share one here:

Good luck!
PJ

id    time    cmt
1     0          1
1     0           2
1     0           3

setwd('C:/biphen/physiol/bootstrap/test1.nm7")
datr<-read.table(file="param.tbl",header=T)
#> Warning in file(file, "rt"): cannot open file 'param.tbl': No such file or
#> directory
#> Error in file(file, "rt"): cannot open the connection
#fix(datr)
write.csv (datr, file="fitsubj.csv")
#> Error in is.data.frame(x): object 'datr' not found

Created on 2019-10-17 by the reprex package (v0.3.0)

Hi,

I see you tried the reprex package, but the output you're sharing makes even less sense than the first post :stuck_out_tongue:

I know it can be tricky to explain what you're looking for and share the correct code, but I won't be able to help you if I don't understand the problem and have some code I can run and look into...

Again, your question is about a loop, but I don't even see a loop or where there should be one. It seems you are trying to read in files, do some transformations and save the results again in a file.

Just try and create a good reprex one more time
PJ

Hope this clarifies what I need to do.
setwd("C:/biphenpeds_nda205831/physiolpopfit/bootstrap/test1.nm7/")
getwd()
#> [1] "C:/biphenpeds_nda205831/physiolpopfit/bootstrap/test1.nm7"

datr<-read.table(file="param.tbl",header=T)
fix(datr)
write.csv (datr, file="fitsubj.csv")


setwd("C:/biphenpeds_nda205831/physiolpopfit/bootstrap/test2.nm7/")

datr<-read.table(file="param.tbl",header=T)
fix(datr)
write.csv (datr, file="fitsubj.csv")

Created on 2019-10-18 by the reprex package (v0.3.0)

Reading a *.tbl file from several directories and outputting a *.csv file to the same directory

#CODE WORKS FOR ONE FILE
getwd()
#> [1] "C:/Users/Andre Jackson/AppData/Local/Temp/RtmpMvjiYL/reprex71203d3e4ee0"
setwd("C:/biphenpeds_nda205831/physiolpopfit/bootstrap/test1.nm7/")

datr<-read.table(file="param.tbl",header=T)
write.csv (datr, file="fitsubj.csv")

#CODE DOES NOT WORK FOR MULTIPLE TESTi FILES
for (i in seq(from=1, to=5,by=1)) i
("C:/biphenpeds_nda205831/physiolpopfit/bootstrap/testi.nm7/")
#> [1] "C:/biphenpeds_nda205831/physiolpopfit/bootstrap/testi.nm7/"
datr<-read.table(file="param.tbl",header=T)
write.csv (datr, file="fitsubj.csv")

Created on 2019-10-19 by the reprex package (v0.3.0)

You can do something like this

library(tidyverse)

setwd("C:/biphenpeds_nda205831/physiolpopfit/bootstrap/")

file_list <- list.files(pattern = "param\\.tbl$",
                        full.names = TRUE,
                        recursive = TRUE)

walk(file_list,
     ~ write.csv(
         read.table(file = .x, header=T),
         file = str_replace(.x, "param\\.tbl$", "fitsubj\\.csv")
     )
)

I ran your suggested code and there were no errors but there were also no files output into the folders. Do you have any idea what may have happened in the execution?

Yeah, I made a mistake on the regex part for the file name, I have fix it and I think is OK now, try it again.

Could you please clarify further the error you think that you made?

I could not see any error in file names. Both file names param.tbl and fitsubj.csv are okay.

Because I already changed it, before it was like this

file = str_replace(.x, "param\\.csv$", "fitsubj\\.csv")

Note the .csv extension for "param" file.

You can see the change history for a post by clicking on the little "pencil" icon on the top right corner.
image

I noted your changes in the code but when I looked in my test(i).nm7 folders there was no fitsubj.csv file.

Hard to know what the problem could be in your case, it works for me with dummy param.tbl files and test(i).nm7 folders
Any chance you could provide a link to a couple of those files?

Let me know how that can be done and I will post two test(i).nm7 folders with the param.tbl files enclosed. I have not posted for an R issue previously, but realize it is possible.

I looked at the R help and found this link :slight_smile:
csv - Read several files in different directories in r - Stack Overflow

It has the code which I need. However the answer list all of the files/folders which have different names which seems to be a brute force method.
Can this be done with your suggested code without listing all of the folders which have different names (e.g., test(i).nm7?

I have placed a param.tbl file into the GITHUB repository under the public directory jacksonan/newone for you to view.

After I posted the file on GITHUB I realized that it contained proprietary data so I deleted the file. I will endeavor to edit your code to work on my issue.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.