You can use pmap_dfr
to apply a function across the rows and then row bind the resulting list into a tibble object. The function will match arguments to column names, the rest of the row values will be captured in the ellipsis ...
.
library(purrr)
library(dplyr)
pmap_dfr(df, function(Weight, Days, ...) c(..., setNames(rep(Weight, Days), 1:Days))) %>%
mutate(across(3:last_col(), as.numeric))
Because vectors are atomic in R c()
will coerce everything in the row to be character. So the mutate converts the newly created columns back to numeric.
setNames
is used to name the newly created columns, which is required to bind by row.
Output
Name School `1` `2` `3` `4` `5` `6` `7`
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Antoine Bach 0.03 0.03 0.03 0.03 0.03 NA NA
2 Antoine Ken 0.02 0.02 0.02 0.02 0.02 0.02 0.02
3 Barbara Franklin 0.04 0.04 0.04 NA NA NA NA
Note: pmap_dfr
is from the purrr
package, and mutate
, across
, and last_col
are all from dplyr
.
How it works
When you use pmap
in the way above the named function arguments will be matched to columns with the same name. So Weights
and Days
as function arguments are matched to those columns with the same name in each row.
The ...
collects the remaining columns that are still passed to the function, but are unused (by name) in the function. Essentially, the ellipsis collects Name
and School
in your case.
Since Name
and School
already have names they are passed to c()
first to maintain your column order. In addition we combine the other values and give them names as well. The output for a single row is then this:
Name School 1 2 3 4 5 6
"Antoine" "Bach" "0.03" "0.03" "0.03" "0.03" "0.03" NA
7
NA
The output of pmap
is a list. _dfr
is a specific function to row bind (hence the r
) these list elements into a dataframe/tibble (hence the df
).