Putting print from a "for loop" into a list/vektor/dataframe

I have a for loop that i let go through a list (xlist) with a different output for each iteration of the code. When i use print i get the results for each iteration but i am looking for help to get my printout into a format i can further work with.
The code for the loop looks like this:

for (i in 1:381)
{ print(nrow(mydata %>% add_column(f=replace_na(mydata$Date_1,xlist$xlist[i]))%>% 
              filter(Date_2 %in% (xlist$xlist[i]- ddays(x=7)):xlist$xlist[i]) %>% 
              filter(Date_3 %in% (xlist$xlist[i]- ddays(x=7)):xlist$xlist[i]) %>% 

ist prints out all iterations but i can not put it into a list or a dataframe. How do i do that?

How about using map() instead of for() to handle the iterations. All of the results will be neatly available in a numeric vector after running, currently loaded in a variable called at in the sample code here:


at <- map_dbl(
    #place your code here, replace "i", with ".x"
    mtcars %>% 
      sample_frac(0.2) %>% 
      summarise(max(mpg)) %>% 

#> [1] 21.4 24.4 27.3 30.4 21.0 21.5

Created on 2021-10-13 by the reprex package (v2.0.1)

This topic was automatically closed 21 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.