How do I rbind(df) inside purrr::map?

Here is example pseudo code. It did not work as it would in a for loop.

# init an empty df
df = data.frame()

map(1:10, ~ {
  df_result_one_iteration = do_some_stuff(.x) # .x is from 1:10, right?
  df = df |> rbind(df_result_one_iteration)
})

It just prints df instead of actually outputting it. How do I make it rbind()?

I cant guess what your intention is so i cant offer to try help you get there. It seems like you are trying to make the result of one iteration dependent on others which if is the case you shod stick to for loops and not map.

I see.

What if it's not dependent on others? The df output would be different for each iteration in 1:10, but have consistent columns, and row order does not matter -- they can all be combined into the df in any order all at once/in parallel. Would I still not be able to use rbind()?

If I understand you correctly, there is no need to use rbind() iterate using map_dfr() instead of map() and the result will be a single data frame with all rows produced by each iteration.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.