I have a data frame in which each row contains values for specific ages (0, 6, 15, 24, 36 months). I can interpolate values for one row to other ages using approx
. How can I apply this across all rows? I have tried various things with rowwise()
, but they don't work.
library(tidyverse)
library(stats)
care_distribution_table = "Age,M0,M6,M15,M24,M36
Mother,100.00%,36.89%,29.58%,29.61%,26.64%
Father,0.00%,11.74%,14.70%,12.23%,11.40%
Other relative,0.00%,15.80%,14.00%,11.55%,10.20%
In-home nonrelative,0.00%,7.94%,8.89%,7.33%,6.48%
Home-based childcare,0.00%,18.62%,20.69%,21.82%,17.38%
Center childcare,0.00%,9.00%,12.15%,17.46%,27.90%"
care_dist = read.csv(text = care_distribution_table, row.names = 1) |>
# Convert from % to float
mutate(across(everything(), ~as.numeric(trimws(.x, whitespace = '%'))) / 100) |>
# Make sure columns add to 1 (i.e. handle rounding error)
mutate(across(everything(), ~.x/sum(.x)))
# This interpolates one row
approx(x = c(0,6,15,24,36), y = care_dist['Mother',], xout = 0:36)
# This gets me most of the way
care_dist |> rowwise() |> group_map(~approx(x = c(0,6,15,24,36), y = .x, xout = 0:36)$y)
# This fails
care_dist |> rowwise() |> group_modify(~df(z = approx(x = c(0,6,15,24,36), y = .x, xout = 0:36)$y))
if CC
is the output of your group_map()
call, then
bind_cols(care_dist, do.call(rbind, CC))
will get you a wide-format tibble that includes all the approximated values (with names ...6
, ...7
, ... maybe you want to write a wrapper function that gives these better names?)
(I'm not sure why I needed do.call(rbind, ...)
instead of bind_rows()
.)