'names' attribute [1] must be the same length as the vector [0]

I have a number of pdf files (these are "scanned") in a folder ( "C:/Users/Documents/files_i_want" ). All the pdf files have different names. I am trying to import them all into R at the same time, by using the following command: pdftools::pdf_convert


#Get the path of filenames

filenames <- list.files("C:/Users/Documents/files_i_want", full.names = TRUE)

#Read them in a list

list_data <- lapply(filenames,  pdftools::pdf_convert)

#Name them as per your choice (df_1, df_2 etc)

names(list_data) <- paste('df', seq_along(filenames), sep = '_')

#Create objects in global environment.

list2env(list_data, .GlobalEnv)

This returns the following error:

Error in names(list_data) <- paste("df", seq_along(filenames), sep = "_") : 
  'names' attribute [1] must be the same length as the vector [0]

Does anyone know why this error is being produced?



This error occurs when the length of elements on both sides of an assignment are not the same

list_data = character()
#> [1] 0
names(list_data) = "test"
#> Error in names(list_data) = "test": 'names' attribute [1] must be the same length as the vector [0]

Created on 2021-08-01 by the reprex package (v2.0.0)

This example shows that list_data is empty (i.e. of length 0) and trying to assign something of length 1 ("test") will produce this error.

Hope this helps,

1 Like

Thank you for your reply!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.