0

I have got multiple files in txt.gz format in a folder and wanted to run below command in R on all these files.I also wanted to add z_score in name while writing these files as shown below.

Could you please help me how to run below command for all files in R. I NEVER RUN ANY loop in R.

> Data = read.table(gzfile("sample1.txt.gz"),sep="", header = TRUE)
 
> colnames(Data) 
 
> data_new <- Data
> data_new$z_score <- data_new$Effect/data_new$StdErr
> colnames(data_new) 
      
> write.table(data_new,gzfile("sample1.z_score.txt.gz"))

###for loop

data_files <- list.files(“/Users/Joach/Desktop/MyFolder", pattern =*.txt.gz)   
#Identify file names
data_files 

for(FILES in data_files) { 
Data <- read.table(gzfile(data_files[FILES],sep="", header = TRUE),
data_new <- Data,
data_new$z_score <- data_new$Effect/data_new$StdErr,
write.table(data_new,gzfile(“*.txt.gz"),
file.rename(list.files(pattern="*.txt.gz"), paste0("z_score",txt.gz))
}

Many thanks,

bioinfonext
  • 119
  • 7
  • 1
    See https://stackoverflow.com/questions/5758084/loop-in-r-to-read-many-files and https://stackoverflow.com/questions/3764292/loading-many-files-at-once for loop and lapply approaches. – sashahafner Jan 06 '23 at 11:56
  • Especially if you're familiar with loops in other languages, try the R help, ie. `?\`for\`` – Marcus Jan 06 '23 at 15:39
  • Could anyone please help to correct above R loop. Many thanks – bioinfonext Jan 09 '23 at 09:26

0 Answers0