MWE:
library(stats)
months <- 1:12
months_plus_one <- months |> append(months[length(months)] + 1)
hours_list <- lapply(months_plus_one, (m)(stepfun(c(m), c(0, 50))(months)))
hours <- do.call(rbind, hours_list)
This generates a matrix, where each row comes from a call to stepfun.
This feels like a messy way of turning a vector (months
) into a matrix (hours
).
- Is there a cleaner way of doing this, which exploits the fact that R is designed to work with vectorised functions?
- If
months
were a column of a dataframe, would tidyverse provide a nicer syntax?