Weighted multiplication of a vector with intervals

Viewed 12

I have a data frame

a <- data.frame(list(wind_speed=c(21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0), power=c(29, 171, 389, 703, 1139, 1682, 2205, 2700, 2700)))

which shows the (theoretical) relationship between wind speed and power, e.g. if wind speed is 21 then power is 29. Now the two measured wind speeds are

b <- c(21.5, 27.2)

I would like the effective power production, e.g. 21.5 falls in between 21 and 22 and therefore the result is (29+171)/2. Or for 27.2: 0.8*2205+0.2*2700=2304

1 Answers

Use base function approxfun to create an interpolation function and then pass it the new values.

a <-  data.frame(
  wind_speed=c(21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0), 
  power=c(29, 171, 389, 703, 1139, 1682, 2205, 2700, 2700)
)

f <- with(a, approxfun(wind_speed, power))
xnew <- c(21.5, 27.2)
f(xnew)
#> [1]  100 2304

Created on 2022-09-24 with reprex v2.0.2

Related