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,