Search code examples
rtidyverse

Iterating a pipe over objects in a list


I have a series of data frames, and I want to run several functions on them using a pipe. I created a list of the objects as follows (in reality there are around 30 objects):

a <- 1:10
b <- 5:14
c <- 10:19
d <- c(NA, 4:12)
e <- 60:69
f <- 7:16
d1 <- tibble(a, b, c, d, e)
d2 <- tibble(a, b, c, e, f)
d_list <- c(d1, d2)

Then I tried to use a for loop to pipe functions over the data frames:

for(d in d_list){
  d %>% 
    select(c(1, 3:5)) %>% 
    na.omit()
}

If I run the pipeline on one of the tibbles outside of the for loop, it works fine. But inside the loop I get an error: Error in UseMethod("select") : no applicable method for 'select' applied to an object of class "c('integer', 'numeric')". I guess this means that the parser does not understand that I want to apply the functions to the data frames in d_list; but I don't understand why.

Thanks,

Peter


Solution

  • Once you've fixed the issue identified in @bretauv's answer (list(d1, d2) instead of c(d1, d2)) then try:

    d_list |> 
       lapply(select, c(1,3:5)) |> 
       lapply(na.omit)
    

    lapply takes a list as input, applies the specified function (with arguments) to each element and returns the results as a list.

    Output is a list:

    
    
    # A tibble: 9 × 4
          a     c     d     e
      <int> <int> <int> <int>
    1     2    11     4    61
    2     3    12     5    62
    3     4    13     6    63
    4     5    14     7    64
    5     6    15     8    65
    6     7    16     9    66
    7     8    17    10    67
    8     9    18    11    68
    9    10    19    12    69
    
    [[2]]
    # A tibble: 10 × 4
           a     c     e     f
       <int> <int> <int> <int>
     1     1    10    60     7
     2     2    11    61     8
     3     3    12    62     9
     4     4    13    63    10
     5     5    14    64    11
     6     6    15    65    12
     7     7    16    66    13
     8     8    17    67    14
     9     9    18    68    15
    10    10    19    69    16