I have multiple objects that I created which include data I need for each triangle. I now want to create triangles and export to csv, but I have so many datasets that I’d like to do this in a loop. Bonus points if I can also name each csv the same as the triangle name (for example below, LiabilityTriangleMonthly). My current code that works on a one-by-one basis is as follows:
install.packages("ChainLadder")
install.packages("writexl")
install.packages("openxlsx")
install.packages("data.table")
install.packages("xlsx")
install.packages("sqldf")
library(ChainLadder)
library(writexl)
library(openxlsx)
library(data.table)
library(dplyr)
library(readxl)
library(sqldf)
LiabilityData_Monthly<-read.csv.sql("//filepath/FullTriangle.csv" , sql = "select * from file where LineofBusiness=194")
Liability_Triangle_Monthly = as.triangle(LiabilityData_Monthly,
dev = "MaturityMonth",
origin = "AY",
value = "IncurredTotal")
LiabilityTriangleMonthly=as.matrix(Liability_Triangle_Monthly)
LiabilityTriangleMonthly[is.na(LiabilityTriangleMonthly)]<-0
write.csv(LiabilityTriangleMonthly,"//filepath/LiabilityTriangleMonthly.csv")
For reference, the names of 2 of my objects:
LiabilityData_Monthly
PhysDamData_Monthly
Below is my sample dataset (FullTriangle.csv) that I’m pulling my data from:
AY | MaturityMonth | IncurredTotal | LineOfBusiness |
---|---|---|---|
2023 | 3 | 1000 | 194 |
2023 | 6 | 5000 | 194 |
2023 | 9 | 10000 | 194 |
2023 | 12 | 15000 | 194 |
2023 | 15 | 20000 | 194 |
2024 | 3 | 100 | 194 |
2023 | 3 | 100 | 212 |
2023 | 6 | 500 | 212 |
2023 | 9 | 1000 | 212 |
2023 | 12 | 1500 | 212 |
2023 | 15 | 2000 | 212 |
2024 | 3 | 150 | 212 |
2023 | 3 | 1500 | 193 |
2023 | 6 | 4000 | 193 |
2023 | 9 | 9000 | 193 |
2023 | 12 | 12000 | 193 |
2023 | 15 | 16000 | 193 |
2024 | 3 | 200 | 193 |
Tried to create a list to loop through… but very new to R and need a simpler version than what I’ve seen on this site already.
JustAskingQuestions is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.