Home > Net >  How do I find a Problem file in function?
How do I find a Problem file in function?

Time:05-02

I'm trying to import ~160 files and reshape them, but beforehand I'm trying to bind a column to from a vector to them of the same length. They are all supposed to have 43 columns, but one has 41. Leading to the error below. Any chance I could identify which file that is?

  readxl::read_excel(paste0(path,
                            file), col_names = TRUE)%>%
    rename(category = 1)%>%
    bind_cols(category2,)
})

Error in `bind_cols()`:
! Can't recycle `..1` (size 41) to match `..2` (size 43).
Run `rlang::last_error()` to see where the error occurred.

CodePudding user response:

you could use map() to load your data into list, give the the name of the file as list name using set_names() and keep only lists with 41 columns using keep(), i.e.

library(purrr)
library(dplyr)
file_list <- list.files(pattern='*.xls', path = "yourdatapath", full.names = T)
map(file_list, ~ readxl::read_excel(.x)) %>%
  set_names(file_list) %>% 
  keep(~ncol(.x)==41)
  •  Tags:  
  • r
  • Related