LIC_template <- LIC_template_v1 %>%
mutate(
source system = "PROL",
expectation = 50,
reporting date = reporting_date,
company = 101,
Trading Partner = "",
Settlement Date = Settlement_Date,
Risk Adjustment = 0.00,
Accident Year = Accident_Year
) %>%
group_by(Currency,GIC,Accident_Year,source system,expectation,reporting date) %>%
mutate(
Non-InvC part of Claims = sum(other_part_of_IBNS),
Investment Component = sum(Investment_component)
) %>%
ungroup() %>%
select(source system,expectation,Currency,reporting date,company,GIC,Trading Partner,Accident Year,Settlement Date,
Non-InvC part of Claims,Risk Adjustment,Investment Component) %>%
distinct() %>%
arrange(GIC,Accident Year)
Assistant
Your code uses the dplyr package in R to transform a dataset named LIC_template_v1 into a specific format with various columns calculated or reassigned, and then arranges and selects unique rows based on specific criteria. Here is an explanation of each step involved in your script:
Loading Libraries and Dataset: Ensure you load the dplyr library and have the dataset LIC_template_v1 available in your environment.
Initial Transformation with mutate:
Adds or modifies several columns:
source system: set to the constant string "PROL".
expectation: set to the constant value 50.
reporting date: assigned the value of reporting_date.
company: set to the constant value 101.
Trading Partner: set to an empty string "".
Settlement Date: assigned the value of Settlement_Date.
Risk Adjustment: set to the constant value 0.00.
Accident Year: assigned the value of Accident_Year.
Grouping the Data:
Groups the data by Currency, GIC, Accident_Year, source system, expectation, and reporting date.
Calculating Sums with another mutate:
Within each group:
Non-InvC part of Claims: calculates the sum of other_part_of_IBNS.
Investment Component: calculates the sum of Investment_component.
Ungrouping:
The ungroup function is used to remove the grouping, preparing the data for the next steps.
Selecting Columns:
Selects only the specified columns to retain in the final dataset:
source system, expectation, Currency, reporting date, company, GIC, Trading Partner, Accident Year, Settlement Date, Non-InvC part of Claims, Risk Adjustment, and Investment Component.
Distinct Rows:
Ensures that only unique rows are retained in the dataset using distinct().
Arranging the Data:
Sorts the data first by GIC and then by Accident Year.
Here's the complete R script written in a clearer way, including a minimal example with made-up data to demonstrate how it works:
r
library(dplyr)
# Assuming LIC_template_v1 is already loaded in your environment
LIC_template <- LIC_template_v1 %>%
mutate(
`source system` = "PROL",
expectation = 50,
`reporting date` = reporting_date,
company = 101,
`Trading Partner` = "",
`Settlement Date` = Settlement_Date,
`Risk Adjustment` = 0.00,
`Accident Year` = Accident_Year
) %>%
group_by(Currency, GIC, Accident_Year, `source system`, expectation, `reporting date`) %>%
mutate(
`Non-InvC part of Claims` = sum(other_part_of_IBNS, na.rm = TRUE),
`Investment Component` = sum(Investment_component, na.rm = TRUE)
) %>%
ungroup() %>%
select(`source system`, expectation, Currency, `reporting date`, company, GIC, `Trading Partner`, `Accident Year`,
`Settlement Date`, `Non-InvC part of Claims`, `Risk Adjustment`, `Investment Component`) %>%
distinct() %>%
arrange(GIC, `Accident Year`)
# Display the first few rows of the resulting dataset
head(LIC_template)
Remember to replace reporting_date, Settlement_Date, Accident_Year, other_part_of_IBNS, and Investment_component with the actual column names from your dataset if they are different. The na.rm = TRUE inside sum() ensures that NA values are ignored in the summation.