使用Purrr

时间:2019-07-18 23:02:38

标签: r function for-loop dplyr purrr

我正在R中构建一个保险评估者。我想构建一个带有多个参数的单个(有时很复杂)的函数,然后在每个保单持有人(数据行)上迭代该函数。我将为此项目构建大约200个这些功能。该函数运行良好,它实际上只是在费率表中查找值,并在何时,什么时候以及如何需要时加或乘。我的问题是,当我有两个或多个参数直接从数据帧直接馈入函数时,如何映射,循环或以其他方式迭代此函数?

我以为我的编码可以用,但是后来我意识到map(或map_dbl)仅拉入了我函数中第二个参数的第一个元素。

library(dplyr)
library(readr)
library(purrr)

#dummy data frames that represent a rating table, and a policy
#holder dataset

data_frame_Rate_Table<-data.frame("Policy_Class"=c("red", "white","blue"),"Rate"=c(3,9,19),"Factor_1"= 1:3,"Factor_2"=7:9)
data_frame_Policyholders<-data.frame("Policy_number"=1:10,"Policy_Class"=rep(c("red","red","white","blue","blue"),2),"Risk"=c(rep("High",5),rep("Low",5)),"Lapse"=rep(c("Y","N"),5))

function_example<-function(x,y,z){
  Policy_Class<-x
  Risk<-y
  Lapse<-z
  Rate<-ifelse(Policy_Class=="red",
              data_frame_Rate_Table[data_frame_Rate_Table['Policy_Class']==Policy_Class,"Rate"]*data_frame_Rate_Table[data_frame_Rate_Table['Policy_Class']==Policy_Class,"Factor_1"]+
              (ifelse(Risk=="High",3,1))*data_frame_Rate_Table[data_frame_Rate_Table['Policy_Class']==Policy_Class,"Factor_2"]+ifelse(Lapse=="Y",10,0),
        ifelse(Policy_Class=="white",
              data_frame_Rate_Table[data_frame_Rate_Table['Policy_Class']==Policy_Class,"Rate"]*data_frame_Rate_Table[data_frame_Rate_Table['Policy_Class']==Policy_Class,"Factor_1"]+
              (ifelse(Risk=="High",5,1))*data_frame_Rate_Table[data_frame_Rate_Table['Policy_Class']==Policy_Class,"Factor_2"]+ifelse(Lapse=="Y",15,0),
        ifelse(Policy_Class=="blue",
              data_frame_Rate_Table[data_frame_Rate_Table['Policy_Class']==Policy_Class,"Rate"]*data_frame_Rate_Table[data_frame_Rate_Table['Policy_Class']==Policy_Class,"Factor_1"]+
                (ifelse(Risk=="High",10,1))*data_frame_Rate_Table[data_frame_Rate_Table['Policy_Class']==Policy_Class,"Factor_2"]+ifelse(Lapse=="Y",33,0))))
    Rate
    }

我尝试过:

result<-map_dbl(data_frame_Policyholders$Class,function_example, data_frame_Policyholders$Risk,data_frame_Policyholders$Lapse)

但这不是我所需要的。

预期结果是:

#copy and paste this coding into R to get the actual 
#values that should go into the vector
function_example("red","High","Y")
function_example("red","High","N")
function_example("red","Low","Y")
function_example("red","Low","N")
function_example("white","High","Y")
function_example("white","High","N")
function_example("white","Low","Y")
function_example("white","Low","N")
function_example("blue","High","Y")
function_example("blue","High","N")
function_example("blue","Low","Y")
function_example("blue","Low","N")

但是我显然不能输入每种组合

我需要迭代每个保单持有人并以排队的数字矢量产生结果的功能。 purrr还能做到吗?有没有更好的方法去这里?

1 个答案:

答案 0 :(得分:2)

好吧,第一件事是将数据读取为字符而不是因子。其次,由于您对函数有多个输入,因此需要/>而不是仅pmap

map

数据

library(dplyr)
library(purrr)

data_frame_Policyholders %>%
    mutate(new = pmap_dbl(list(Policy_Class, Risk, Lapse), function_example))


#   Policy_number Policy_Class Risk Lapse new
#1              1          red High     Y  34
#2              2          red High     N  24
#3              3        white High     Y  73
#4              4         blue High     N 147
#5              5         blue High     Y 180
#6              6          red  Low     N  10
#7              7          red  Low     Y  20
#8              8        white  Low     N  26
#9              9         blue  Low     Y  99
#10            10         blue  Low     N  66