Hi there,
I have two dataframes pool_performance
, to which I need to add info from payout_scheme
based on data in pool_performance
. Using rowwise
mutate
ifelse
, the operation returns the row index of the value I'm looking for in payout_scheme
, as opposed to the desired value in payout_scheme$policy_name
.
What is wrong with the below?
Thanks in advance!
library(tidyverse)
payout_scheme <- data.frame(
a_rate_floor = rep(c(.7,.76,.81,.86,.91,.96),2),
a_rate_ceil = rep(c(.75,.8,.85,.9,.95,1.0),2),
a_count_floor = c(rep(36,6),rep(41,6)),
a_count_ceil = c(rep(40,6),rep(45,6)),
payout = c(250,280,300,325,350,420,320,395,540,570,650,700),
policy_name = c(paste0("A",seq(1,6,1)),paste0("B",seq(1,6,1)))
)
set.seed(12345)
pool_performance <- data.frame(
id = seq(1,36,1),
a_rate = runif(36, min = .5, max = .9),
a_count = floor(runif(36, min = 35, max = 44))
) %>%
mutate(
a_rate_hit = ifelse(a_rate > min(payout_scheme$a_rate_floor),1,0),
a_count_hit = ifelse(a_count > min(payout_scheme$a_count_floor),1,0)
) %>%
rowwise() %>%
mutate(
qualified = ifelse(a_rate_hit == 1 && a_count_hit == 1,1,0),
policy_1 = ifelse(qualified == 1, payout_scheme$policy_name[which(
payout_scheme$a_rate_floor<a_rate
& payout_scheme$a_rate_ceil>a_rate
& payout_scheme$a_count_floor <a_count
& payout_scheme$a_count_ceil >a_count)],NA)
)
Applying the same logic outside the chained mutate command returns desired values; one example below.
payout_scheme$policy_name[which(
payout_scheme$a_rate_floor<pool_performance$a_rate[1]
& payout_scheme$a_rate_ceil>pool_performance$a_rate[1]
& payout_scheme$a_count_floor < pool_performance$a_count[1]
& payout_scheme$a_count_ceil > pool_performance$a_count[1])]