In base R you could do:
d <- tibble(a = letters[1:3], b = 3:5)
d[rep(1:nrow(d), d$b), ]
I wasn't aware of uncount until I read Nir's answer. By analogy with the base R solution, slice can also be used to repeat rows:
d %>% slice(rep(1:nrow(.), b))
Regarding walk: walk iterates just like map, but it doesn't return the list. This can be useful when you want to perform some action, but don't need anything returned. For example, the code below writes a data frame to an Excel file and conditionally formats some of the columns.
library(openxlsx)
wb=createWorkbook()
sht=addWorksheet(wb, "Data")
writeData(wb, sht, mtcars)
map(c(1,3,7),
~conditionalFormatting(wb, sht, cols=.x, rows=1:nrow(mtcars) + 1,
rule=sprintf(">%s", median(mtcars[,.x])))
)
saveWorkbook(wb, "myfile.xlsx")
But note that the map step returns an empty list:
[[1]]
[1] 0
[[2]]
[1] 0
[[3]]
[1] 0
If you use walk instead
walk(c(1,3,7),
~conditionalFormatting(wb, sht, cols=.x, rows=1:nrow(mtcars) + 1,
rule=sprintf(">%s", median(mtcars[,.x])))
)
then the "side effect"--the conditional formatting of the Excel file--is still implemented, but without the list being returned.
I don't use walk very often and I don't know if this is a particularly good example, but it was on my mind, as I was doing some conditional formatting today.