I'm trying to create a number sequence from a single row when I know the maximum number.
I've actually done this successfully using a loop but want to know how to do this using the apply
family of functions and so reduce the number of steps and intermediate objects created.
I've tried combinations of apply
, lapply
and mapply
to no avail.
Here's the data:
df <- structure(list(location = c("doncaster", "dover", "dundee", "eastbourne"), num.pages = c(6, 3, 3, 4)), row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame"))
Here's the loop:
for(i in seq_along(df$location)) {
assign(
paste0("loc_seq_", df$location[[i]]),
seq(from = 1, to = df$num.pages[[i]]) %>%
enframe(name = NULL, value = 'page.seq') %>%
mutate(location = df$location[[i]])
)
}
I then use:
location_sequence <- bind_rows(mget(ls(pattern = "loc_seq_*")))
... to produce the correct result.
How can I use the apply
family to get the same result from my df
starting point above please?