Q: function sapply error : subscript out of bounds in R I am a beginner with R and struggling to resolve the following issue.
I am currently trying to build my corpus in R to run topic modelling. My dataset consists of +55,000 newspaper articles. An error arises with the 'sapply' command.
DATA <- '/projects/netzero-transition-frames/DATASET'
texts <-readtext(DATA)
files <- list.files(DATA, recursive=TRUE) 
print(files)
#define metadata based on file path
metadataraw <- data.table(str_match(files,"([\\w\\s]+)\\/(\\d+)\\/(\\w+)\\/(.+)")[,2:5])
metadataraw <- metadataraw %>% 
  rename(
    newspaper = V1,
    year = V2,
    month = V3, 
    doc_id = V4
  )
print(metadataraw)
common_column_names <- intersect(names(texts), names(metadataraw))
print(common_column_names)
#common column is doc_id
articles_metadata <- merge(texts, metadataraw, by=common_column_names, all.dataframe_1=TRUE)
articles_metadata$text <- sapply(str_split(articles_metadata$text, coll("Body")), "[[", 2)
articles_metadata$text <- sapply(str_split(articles_metadata$text, coll("Load-Date")), "[[", 1)
corpus <- corpus(articles_metadata)
summary(corpus)
print(corpus)
docvars(corpus)

The following two lines are meant to specify that for each article, the text should only be kept from the words starting after "Body" and before "Load-Date". The "[[", 2) specifies to keep row 2 of the split which contains the text for the words after "Body"
articles_metadata$text <- sapply(str_split(articles_metadata$text, coll("Body")), "[[", 2)
articles_metadata$text <- sapply(str_split(articles_metadata$text, coll("Load-Date")), "[[", 1)

The code worked when I ran it on a subset of data (300 articles) but when I ran it on the entire data set (+55,000) , I got the following error message:
"Error in FUN(X[[i]], ...) : subscript out of bounds
Calls: sapply -> lapply"

Does anyone have any suggestions how this can be resolved? Or if there is an easier way to specify that for the text column, the text for each article should remove all words before 'Body' and all words after 'Load-Date'
These are the libraries I have installed:
library(stm)
library(quanteda)
library(readtext)
library(textreadr)
library(stringr)
library(dplyr)
library(tidyverse)
library(data.table)
library(tidytext)
library(tokenizers)
library(tm)
library(ggplot2)
library(geometry)
library(rsvd)
library(Rtsne)
library(furrr)
library(future)

Thanks so much!
