0

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.

2
  • You will need to provide some sample data. Commented May 17, 2024 at 20:42
  • 1
    Also kindly define the needed packages. Where is as.triangle from? Try to make your example minimal and self-contained. Commented May 17, 2024 at 21:10

1 Answer 1

0

Assuming you need to run subsets across the LineOfBusiness column, consider defining a method to run one split, then iteratively call the method passing subset splits using by (object-oriented wrapper to tapply). Also, to properly name CSVs, below builds a small lookup data frame to merge to main data frame:

# HANDLE DATA SETS
FullTriangle <- read.csv("//filepath/FullTriangle.csv")

LineCodes <- data.frame(
  LineOfBusiness = c(193, 194, 212),
  LineOfBusinessTitle = c("Other_Monthly", "LiabilityData_Monthly", "PhysDamData_Monthly")
)

FullTriangle <- merge(FullTriangle. LineCodes, by="LineOfBusiness")

# USER-DEFINED METHOD
proc_triangle <- function(sub) {
    tri = as.triangle(
        sub, 
        dev = "MaturityMonth", 
        origin = "AY", 
        value = "IncurredTotal"
    )

    tri <- as.matrix(tri)
    tri[is.na(tri)] <- 0
    write.csv(tri, paste0("//filepath/", tri$LineOfBusinessTitle[1], ".csv"))

    return(tri)
}

# BUILD NAMED LIST OF TRIANGLE WITH CSV OUTPUTS
df_list <- by(FullTriangle, FullTriangle$LineOfBusiness, proc_triangle)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.