0

I've got the following code to read several files and append them separately to a single list along with the file name

foo <- function(fname){
fread(fname, skip = 5, header = TRUE, sep = " ") %>% 
mutate(fn = fname)
}

all <- lapply(files, FUN = foo)

After the file is read, I would like to insert a condition in the function which checks for some properties in the file failing which it drops the file along with the filename.

Not strictly related to reading a table but other files also

Edit:

I also use the following efficient method of doing it from here:

all <- setNames(lapply(files, foo), files)
K_D
  • 147
  • 8
  • BTW - better to use `:=` for data.tables than `mutate`, and `[` for chaining, rather than `%>%` – dww Jan 16 '20 at 14:56
  • 2
    Just a note note that `purrr::map_dfr` does the same thing as your current function out of the box if you assign names to your `files` vector. – Konrad Rudolph Jan 16 '20 at 14:56
  • @dww Since this is R, not C, I’d recommend writing this with a focus on *values* rather than statements, i.e. as `return(if (condition) x[, fn := fname] else NULL)`, and then drop the unnecessary `return` altogether. – Konrad Rudolph Jan 16 '20 at 14:57

1 Answers1

0

I tried the following fairly simple option using Filter. I used the condition in the Filter

all <- setNames(lapply(myFiles, function(x) {readLAS(x)}), myFiles)
all <- Filter(function(x) {area(x) >= 640}, all)

Not while running the lapply function but it uses only one extra line of code

K_D
  • 147
  • 8