0

I have a list of n files with identical columns and I want to subset them based on some conditions which are "captured" in a vector a of the same lenght as the file number, n.

    list_files(file_1, file_2, ..., file_n)

    a <- c(1, 2, 3, ....,n).

I need to extract from each file those rows which meet a condition related to the same column in each file (Column_1) and bind them together with

  rez[1] <- list_file[[1]] %>%
       filter(Column_1 == a[1])
  rez[2] <- list_file[[2]] %>%
       filter(Column_1 == a[2]),
  .
  .
  .

I tried a for loop but I got stucked at the results binding (rbind(rez[1], rez[2], ) Any help? Thank you

gogo88
  • 199
  • 1
  • 9
  • 1
    It would be helpful if you could provide us with a reproducible [minimal working example](https://en.wikipedia.org/wiki/Minimal_working_example) that we can copy and paste to better understand the issue and test possible solutions. You can share datasets with `dput(YOUR_DATASET)` or smaller samples with `dput(head(YOUR_DATASET))`. (See [this answer](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#5963610) for some great advice.) – ktiu Jun 22 '21 at 11:17
  • ... or, since you have a list of frames, perhaps `dput(lapply(rez,head)[1:3])`. – r2evans Jun 22 '21 at 11:43

1 Answers1

1

Using data similar to

dummy_df <- data.frame(Column_1 = 1:26, Column_2 = LETTERS)
list_file <- list(dummy_df, dummy_df, dummy_df)
a <- c(11:13)

you could avoid the loop and use

purrr::map2_dfr(list_file, a, ~ dplyr::filter(.x, Column_1 == .y))

returning

  Column_1 Column_2
1       11        K
2       12        L
3       13        M
ktiu
  • 2,606
  • 6
  • 20