# Interpolating Or Spline All Columns Of A Data Frame

## 25 January 2022 - 1 answer

If a data frame has M rows, how can it be interpolated or splined to create a new data frame with N rows? Here is an example:

``````# Start with some vectors of constant length (M=7) with data at each time point t
df <- tibble(t = c(1, 2, 3, 4, 5, 6, 7),
y1 = c(0.0, 0.5, 1.0, 3.0, 5.0, 2.0, 0.0),
y2 = c(0.0, 0.75, 1.5, 3.5, 6.0, 4.0, 0.0),
y3 = c(0.0, 1.0, 2.0, 4.0, 3.0, 2.0, 0.0))

# How to interpolate or spline these to other numbers of points (rows)?
# By individual column, to spline results to a new vector with length N=15:
spline(x=df\$t, y=df\$y1, n=15)
spline(x=df\$t, y=df\$y2, n=15)
spline(x=df\$t, y=df\$y3, n=15)

``````

So by vector this is trivial. Question is, how can this spline be applied to all columns across the dataset with M rows to create a new dataset with N rows, preferably with tidyverse approach, e.g.:

``````df15 <- df %>% mutate(...replace(?)...(spline(x=?, y=?, n=15)... ???))
``````

Again, I would like to have this spline be applied across ALL columns without having to specify syntax that includes column names. The intent is to apply this to data frames with something on the order of 100 columns and where names and numbers of columns may vary. It is of course not necessary to include the t (or x) column in the data frame if that simplifies the approach at all. Thanks for any insight.

`spline` returns a `list`. So, we may loop `across` with `summarise` and then `unpack` the columns (`summarise` is flexible in returning any number of rows whereas `mutate` is fixed i.e. it should return the same number of rows as the input)

``````library(dplyr)
library(tidyr)
library(stringr)
df %>%
summarise(across(y1:y3,  ~spline(t, .x, n = 15) %>%
as_tibble %>%
rename_with(~ str_c(cur_column(), .)))) %>%
unpack(everything())
``````

-output

``````# A tibble: 15 × 6
y1x   y1y   y2x   y2y   y3x   y3y
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1  1    0      1    0      1    0
2  1.43 0.319  1.43 0.404  1.43 0.542
3  1.86 0.468  1.86 0.673  1.86 0.905
4  2.29 0.566  2.29 0.907  2.29 1.18
5  2.71 0.752  2.71 1.21   2.71 1.56
6  3.14 1.18   3.14 1.68   3.14 2.30
7  3.57 1.93   3.57 2.43   3.57 3.33
8  4    3      4    3.5    4    4
9  4.43 4.24   4.43 4.84   4.43 3.83
10  4.86 4.99   4.86 5.85   4.86 3.21
11  5.29 4.56   5.29 5.90   5.29 2.67
12  5.71 3.12   5.71 4.96   5.71 2.29
13  6.14 1.47   6.14 3.46   6.14 1.82
14  6.57 0.269  6.57 1.74   6.57 1.09
15  7    0      7    0      7    0
``````

NOTE: Here, we renamed the columns as the output from `spline` is a `list` with names `x` and `y` and `data.frame/tibble` wants unique column names