I want to open multiple .txt files within RStudio and have created a loop to open these using the for
function. I want to just delete the first line in each document and then save the .txt file again under the same name. I have been able to open one .txt file at a time using this script:
setwd("Document directory")
Test_data<-read.delim("File Name.txt")
colnames(Test_data)<-NULL
Test_data<-Test_data[-c(1),]
write.table(Test_data, file = "File Name 1.txt", sep = "\t",row.names = FALSE, col.names = FALSE)
AND I have been able to create the loop with this script:
library(tidyverse)
library(fs)
setwd("Document directory")
file_paths <-fs::dir_ls("Document directory")
file_paths
file_contents<- list()
#This creates a loop
for(i in seq_along(file_paths)){
file_contents[[i]]<-read.delim(
file = file_paths[[i]]
)
}
View(file_contents)
But I am unsure how to put this together to create a loop to edit each document in the folder I am working in.
Would anyone be willing to help, please?