lapply
What are the performance differences between for-loops and the apply family of functions?
First of all, it is an already long debunked myth that for loops are any slower than lapply. The for loops in R have been made a lot more performant and are currently at least as fast as lapply. That said, you have to rethink your use of lapply here. Your implementation demands assigning to … Read more
Add “filename” column to table as multiple files are read and bound
I generally use the following approach, based on dplyr/tidyr: data = tibble(File = files) %>% extract(File, “Site”, “([A-Z]{2}-[A-Za-z0-9]{3})”, remove = FALSE) %>% mutate(Data = lapply(File, read_csv)) %>% unnest(Data) %>% select(-File)
How to tell lapply to ignore an error and process the next thing in the list?
Use a tryCatch expression around the function that can throw the error message: testFunction <- function (date_in) { return(tryCatch(as.Date(date_in), error=function(e) NULL)) } The nice thing about the tryCatch function is that you can decide what to do in the case of an error (in this case, return NULL). > lapply(dates2, testFunction) [[1]] [1] “2010-04-06” [[2]] … Read more