我有 R 代码可以从一个文档中提取信息。我如何为我的文件夹中的所有文档循环?

I have R code to extract information from one document. How do I loop that for all the documents in my folder?

我有一个包含 txt 文件的文件夹,我想从中提取特定的文本并将它们以单独的列排列到一个新的数据框中。我为一个文件编写了代码,但我似乎无法将其编辑成一个循环,该循环将 运行 遍历我文件夹中的所有文档。

这是我的一个 txt 文件的代码:

    clean_text <- as.data.frame(strsplit(text$text, '\*' ), col.names = "text") %>% 
mutate(text = str_replace_all(text, "\n", " "),
         text = str_replace_all(text, "- ", ""), 
         text = str_replace_all(text,"^\s", "")) %>% 
  
  filter(!text == " ") %>% 
  
  mutate(paragraphs = ifelse(grepl("^[[:digit:]]", text) == T, text, NA)) %>% 
  
  rename(category = text) %>% 
  mutate(category = ifelse(grepl("^[[:digit:]]", category) == T, NA, category)) %>% 
  fill(category) %>% 
  filter(!is.na(paragraphs)) %>% 
  
  mutate(paragraphs = strsplit(paragraphs, '^[[:digit:]]{1,3}\.|\t\s[[:digit:]]{1,3}\.')) %>% 
  unnest(paragraphs) %>% 
  mutate(paragraphs = strsplit(paragraphs, 'Download as PDF')) %>%
  unnest(paragraphs) %>% 
  mutate(paragraphs = str_replace_all(paragraphs, "\t", "")) %>% 
  mutate(paragraphs = ifelse(grepl("javascript", paragraphs), "", paragraphs)) %>%
  mutate(paragraphs = str_replace_all(paragraphs, "^\s+", "")) %>%
  filter(!paragraphs == "") 

如何将其变成一个循环?我知道有类似的问题,但 none 的解决方案对我有用。在此先感谢您的帮助!

我没有使用循环,而是使用 lapply 并且函数具有与循环相同的行为:

my_path <- "C:/Users/SAID ABIDI/Desktop/test/"
my_a <- list.files(path = my_path)

my_function <- function(x) {
  read_file(paste(my_path, my_a[x], sep = ""))
}
my_var <- lapply(1:length(my_a), my_function)

这对你有帮助吗?

将您的代码放入函数中:

extract_info = function(file) {
  ## Add the code you need to read the text from the file
  ## Something like
  ## text <- readLines(file)
  ## or whatever you are using to read in the file
  clean_text <- as.data.frame(strsplit(text$text, '\*' ), col.names = "text") %>% 
  mutate(text = str_replace_all(text, "\n", " "),
           text = str_replace_all(text, "- ", ""), 
           text = str_replace_all(text,"^\s", "")) %>% 
    
    filter(!text == " ") %>% 
    
    mutate(paragraphs = ifelse(grepl("^[[:digit:]]", text) == T, text, NA)) %>% 
    
    rename(category = text) %>% 
    mutate(category = ifelse(grepl("^[[:digit:]]", category) == T, NA, category)) %>% 
    fill(category) %>% 
    filter(!is.na(paragraphs)) %>% 
    
    mutate(paragraphs = strsplit(paragraphs, '^[[:digit:]]{1,3}\.|\t\s[[:digit:]]{1,3}\.')) %>% 
    unnest(paragraphs) %>% 
    mutate(paragraphs = strsplit(paragraphs, 'Download as PDF')) %>%
    unnest(paragraphs) %>% 
    mutate(paragraphs = str_replace_all(paragraphs, "\t", "")) %>% 
    mutate(paragraphs = ifelse(grepl("javascript", paragraphs), "", paragraphs)) %>%
    mutate(paragraphs = str_replace_all(paragraphs, "^\s+", "")) %>%
    filter(!paragraphs == "") 
}

测试您的函数以确保它适用于一个文件:

extract_info("your_file_name.txt")
## does the result work and look right? 
## work on your function until it does

获取您想要的所有文件的列表运行

my_files = list.files()
## by default this will give you all the files in your working directory
## use the `pattern` argument if you only want files that follow
## a certain naming convention

将您的函数应用于这些文件:

results = lapply(my_files, extract_info)