Read a triangle from a CSV file or a workbook
read_triangle.RdReads a triangle from a CSV file or a sheet of an Excel workbook
(.xlsx, .xlsm, .xlsb, .xls) or OpenDocument spreadsheet (.ods),
in Rust: no R package for spreadsheets is needed. Python's
Triangle.read_csv() and Triangle.read_excel().
Usage
read_triangle(
path,
origin,
development = NULL,
columns = NULL,
keys = NULL,
ages = NULL,
sheet = NULL,
format = c("auto", "csv", "excel"),
origin_grain = "Y",
development_grain = "Y",
cumulative = TRUE,
development_is_valuation = FALSE,
comment = NULL
)Arguments
- path
Path to the file.
- origin
Name of the origin column.
- development
Name of the development column (long layout), or
NULLfor a wide layout.- columns
Long: the measure columns, by default every column that is not the origin, the development or a key. Wide: the name of the one measure,
"values"by default.- keys
Names of the key columns, or
NULLfor one segment.- ages
Wide only: the ages in months of the age columns, in column order, in place of their names.
- sheet
The sheet of a workbook,
NULLfor the first.- format
"auto"(by the file extension:.csvand.txtare CSV,.xlsx,.xlsm,.xlsb,.xlsand.odsare workbooks),"csv"or"excel".- origin_grain, development_grain, cumulative
As for
triangle().- development_is_valuation
Long only: whether
developmentholds valuations instead of ages.- comment
A CSV comment character, such as
"#", orNULL.
Value
A triangle.
Details
Long layout (development given): one row per (keys, origin,
development) cell, as triangle() takes a data frame; rows with the same
cell are summed. Wide layout (development = NULL): one row per (keys,
origin), every other column a development age, named by its age in
months (12, 24, ...) unless ages gives them; a repeated origin is
an error.
How cells are written: an origin is a year (1988), a date
(2021-07-15 or 2021-07, or an Excel date cell) or a period label as
tri@origins prints it (2021Q3, 2021H2); an age is a whole number of
months; a valuation (development_is_valuation = TRUE) is a year (its
December), a date or a period label (its last month); a value is a
number, and an empty cell, NA or NaN is not observed. A workbook
sheet's used range starts with the header row. Errors name the file line
(CSV) or the cell and sheet (Excel) and the column.
Examples
path <- tempfile(fileext = ".csv")
writeLines(c("year,12,24", "2020,100,150", "2021,110,"), path)
tri <- read_triangle(path, "year", columns = "paid")
as.matrix(tri)
#> development
#> origin 12 24
#> 2020 100 150
#> 2021 110 NA