seansteele
seansteele

Reputation: 745

Remove data frame with error from list of data frames in R

I'm web scraping and the output is a list of data frames. I've successfully been using the following code to remove any blank tables from the list and combine them.

df_list <- df_list[!is.na(df_list)]
shot_df <- bind_rows(df_list)

I've started to run into some tables where I'm returned an Error instead of a NA, and I"m not sure how to remove the tables with an error. The error returned when I run the above code is:

Error: Argument 5 must have names.

and here is a screenshot of df_list enter image description here

Upvotes: 1

Views: 198

Answers (1)

pseudospin
pseudospin

Reputation: 2767

Try this:

df_list <- df_list[sapply(df_list, function(x) !inherits(x, 'try-error'))]

after you've removed the NAs.

Upvotes: 1

Related Questions