Jedo0w0
Jedo0w0

Reputation: 3

Importing multiple .txt files into r

I need to import multiple .txt files into R. Each file has multiple sentences in it (for eg, "On Monday, I went to the park.") I would like to be able to import all the files in at the same time and then add them to a tibble, so that I can do text analysis on it.

So far, I have tried

#to create vector of txt files
files <- list.files(pattern = "txt$")

# Read all the files and create a FileName column to store filenames
files_list <- files %>%
  set_names(.) %>%
  map_df(read_table2, .id = "FileName")
my_data <- read.delim(file(files))

But I don't know how to actually load the text in each .txt file into the data. When I run this code above, it only reads in the text from one of the files, not all.

I also tried:

sapply(files, read.delim)
mainlist = list()
for (i in 1: length(fileList)) {
  mainlist[[i]] = read.delim(files[i], header = TRUE, sep = "\t")
}

And while it prints out all the info in each .txt file, when I try to put it in a tibble using

mainlist_tib <- tibble(mainlist)

the tibble is empty.

Any assistance would be greatly appreciated!

Edit: Regarding the tibble, I would like for it to have a column for the txt file name and then another column for the text from the file, and then to be able to use the unnest_tokens() function to have a tibble where each row contains only one word. Sort of like in the example from the text mining textbook by Silge and Robinson: https://www.tidytextmining.com/tidytext.html

Upvotes: 0

Views: 349

Answers (1)

Julian
Julian

Reputation: 9260

You could try it like this:

library(dplyr)
library(purrr)

files %>%
  set_names(.) %>%
  map_dfr(~readr::read_table(., col_names = F), .id = "FileName")

Upvotes: 0

Related Questions