问题
I have one data frame and one look up table. What I want is to compare df_dat$value
with df_lookup$threshold
.
If the value
falls into threshold
range, then create a new column transfer
in df_dat
so that its values are
linearly interpolated from the transfer
column in df_lookup
library(dplyr)
df_lookup <- tribble(
~threshold, ~transfer,
0, 0,
100, 15,
200, 35
)
df_lookup
#> # A tibble: 3 x 2
#> threshold transfer
#> <dbl> <dbl>
#> 1 0 0
#> 2 100 15
#> 3 200 35
df_dat <- tribble(
~date, ~value,
"2009-01-01", 0,
"2009-01-02", 30,
"2009-01-06", 105,
"2009-01-09", 150
)
df_dat
#> # A tibble: 4 x 2
#> date value
#> <chr> <dbl>
#> 1 2009-01-01 0
#> 2 2009-01-02 30
#> 3 2009-01-06 105
#> 4 2009-01-09 150
I can manually do it like this but wondering if there is an automatic way based on the values from the df_lookup
table? Thank you.
df_dat %>%
mutate(transfer = case_when(value > 0 & value < 100 ~ 0 + (value - 0)*(15 - 0)/(100 - 0),
value >= 100 & value < 200 ~ 15 + (value - 100)*(35 - 15)/(200 - 100),
TRUE ~ 0)
)
#> # A tibble: 4 x 3
#> date value transfer
#> <chr> <dbl> <dbl>
#> 1 2009-01-01 0 0
#> 2 2009-01-02 30 4.5
#> 3 2009-01-06 105 16
#> 4 2009-01-09 150 25
回答1:
You can use approx
df_dat %>% mutate(transfer = with(df_lookup, approx(threshold, transfer, value))$y)
## A tibble: 4 x 3
# date value transfer
# <chr> <dbl> <dbl>
#1 2009-01-01 0 0
#2 2009-01-02 30 4.5
#3 2009-01-06 105 16
#4 2009-01-09 150 25
回答2:
Another option using roll
:
df_lookup[, m := (transfer - shift(transfer, -1L)) / (threshold - shift(threshold, -1L))]
df_dat[, tx :=
df_lookup[df_dat, on=c("threshold"="value"), roll=Inf,
x.m * (i.value - x.threshold) + x.transfer]
]
data:
library(data.table)
df_lookup <- fread("threshold, transfer
0, 0
100, 15
200, 35")
df_dat <- fread('date, value
"2009-01-01", 0
"2009-01-02", 30
"2009-01-06", 105
"2009-01-09", 150')
来源:https://stackoverflow.com/questions/56520955/how-to-automatically-interpolate-values-for-one-data-frame-based-on-another-look