I'm trying to change the , (comma) to . (dot) in all of my text files that are in a specific folder using R. However I don't want to manually put in the filepath each time. Instead I want to loop over all of the .TXT files in the folder, and make the change in them and then just save them again with the same name at the same place.
As of right now I have problems with the writeLines function where I tried to set the path with a changable variable, this does not seem to work, resulting in the error message:
"Error in writeLines(tx2, path = listFiles[i]) : unused argument (path = listFiles[i])"
This is my curent code draft:
folder_path <- "C:/Users/pathToMyFiles"
setwd(folder_path)
listFiles= list.files(path = "C:/Users/pathToMyFiles", pattern= "*.TXT",
full.names = TRUE)
#print(listFiles)
#print(listFiles[1])
i=1
for (i in length(listFiles)) {
tx <- readLines(listFiles[i])
tx2 <- gsub(pattern = ",", replace = ".", x = tx)
writeLines(tx2, path = listFiles[i])
i <- i + 1
}
When looking at the produced output all of the steps in the code seems to work, except the "writeLines" function.
I would be grateful if somebody knows a workaround this.
All the best!
N