Search code examples
rdplyrpipemagrittr

using pipe and dplyr to append item to list


I have a dataframe mappedAttModel and a list called features. I want to append the piped item. I'm getting Error object (.)not found.

for (i in 1:length(features)){
 mappedAttModel %>%
 dplyr::filter(.data$att == features[[i]]) %>%
 dplyr::select(.data$model) %>%
 purrr::pluck(1,1) %>%
 #append to the list
 ###l1 <- append(l1,.)
 }```

Solution

  • You can use purrr::map to return a list instead of growing one with the for loop. Something like this should work

    l1 = 
      purrr::map(1:length(features),
                 function(i) {
                   mappedAttModel %>%
                     dplyr::filter(.data$att == features[[i]]) %>%
                     dplyr::select(.data$model) %>%
                     purrr::pluck(1,1)
                 }
      )
    

    Note there are several variations of map depending on the type of output you want.