I have a table t with x and y values spaced out between every 10 integers of x (value
column), for example:
value -20 -10 0 24 40 55
1 100 41700 41700 41700 41700 41800 41700
2 90 40200 40600 40700 40800 40800 40800
3 80 39200 39700 39800 40000 40000 39900
4 70 38200 38800 38800 39000 39100 39000
5 60 37200 37800 37900 38000 38200 38200
6 50 36200 36700 36900 37000 37000 37000
7 40 35400 35900 36200 36300 36300 36400
8 30 34600 35300 35600 35800 35800 35900
9 20 33700 34600 34800 35200 35100 35100
10 10 31600 33700 33800 34000 33900 33900
11 0 30000 30000 26500 30700 30100 30100
Right now, the table is 11 rows x 7 columns. My goal is to interpolate values linearly in all 6 columns for each integer between 0 and 100 so that the table in the end is 101 rows x 7 columns.
I am able to interpolate each column separately using the following method:
x <- t$value
y <- t$`-20`
plot(x, y, main = "-20 mv", xlab = "soc", ylab = "temp", pch = 20)
points(approx(x, y, xout = 0:100), col ="blue", pch = 1)
Can anywone suggest a faster method with dplyr
or data.table
or base R with a few commands that will apply this to the entire table?
The data:
dput(t)
structure(list(value = c(100L, 90L, 80L, 70L, 60L, 50L, 40L,
30L, 20L, 10L, 0L), `-20` = c(41700L, 40200L, 39200L, 38200L,
37200L, 36200L, 35400L, 34600L, 33700L, 31600L, 30000L), `-10` = c(41700L,
40600L, 39700L, 38800L, 37800L, 36700L, 35900L, 35300L, 34600L,
33700L, 30000L), `0` = c(41700L, 40700L, 39800L, 38800L, 37900L,
36900L, 36200L, 35600L, 34800L, 33800L, 26500L), `24` = c(41700L,
40800L, 40000L, 39000L, 38000L, 37000L, 36300L, 35800L, 35200L,
34000L, 30700L), `40` = c(41800L, 40800L, 40000L, 39100L, 38200L,
37000L, 36300L, 35800L, 35100L, 33900L, 30100L), `55` = c(41700L,
40800L, 39900L, 39000L, 38200L, 37000L, 36400L, 35900L, 35100L,
33900L, 30100L)), class = "data.frame", row.names = c(NA, -11L
))
With dplyr
you can do:
library(dplyr)
t %>% summarise(across(everything(), ~approx(value, ., xout=0:100)$y))
value -20 -10 0 24 40 55 1 0 30000 30000 26500 30700 30100 30100 2 1 30160 30370 27230 31030 30480 30480 3 2 30320 30740 27960 31360 30860 30860 4 3 30480 31110 28690 31690 31240 31240 5 4 30640 31480 29420 32020 31620 31620 ... 95 94 40800 41040 41100 41160 41200 41160 96 95 40950 41150 41200 41250 41300 41250 97 96 41100 41260 41300 41340 41400 41340 98 97 41250 41370 41400 41430 41500 41430 99 98 41400 41480 41500 41520 41600 41520 100 99 41550 41590 41600 41610 41700 41610 101 100 41700 41700 41700 41700 41800 41700
Or in base R:
do.call(cbind, lapply(t, function(y) {approx(t$value, y, xout=0:100)$y}))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With