I have a directory of text files. I want to read the contents of these text files, line by line into an R dataframe. The text files contain unstructured text. The desired dataframe output is:
file; line
1.txt; "line 1 in 1.txt"
1.txt; "line 2 in 1.txt"
2.txt; "line 1 in 2.txt"
...
I have written the code below, but it leads to errors. I also guess there is a more straightforward way to do this, with for example readr
and dplyr
.
files <- list.files(path="./data", pattern = "*.txt", full.names = TRUE) # read data folder txt files
my_lines <-list() # create temp list for reading lines
df <- data_frame( "file" = character(0), "line" = character(0))
for (file in files){
my_lines <- readLines(file) # read lines from file into a list
for (line in my_lines){
df$file<-file
df$fline<-line
}
}