Reputation: 33
I am trying to figure what data structure used for the figures in the picture below. It is an output from R. What function should I use to understand if it is a matrix, a data.frame() or an array ? I don't know in which of these data structure there is the possibility to name the axis.
install.packages("ChainLadder")
suppressPackageStartupMessages(library(ChainLadder))
genIns
Thank you very much.
I tried to use typeof() but it had just outputted the type of the figure but not of data structure in which they are stored.
Upvotes: 1
Views: 69
Reputation: 76641
Off-topic? First of all, you don't need to load a package to access one of its data sets. You can load it with
data(GenIns, package = "ChainLadder")
As for the question, to know what kind of object you have start by seeing its class and structure.
class(GenIns)
#> [1] "triangle" "matrix"
str(GenIns)
#> 'triangle' int [1:10, 1:10] 357848 352118 290507 310608 443160 396132 440832 359480 376686 344014 ...
#> - attr(*, "dimnames")=List of 2
#> ..$ origin: chr [1:10] "1" "2" "3" "4" ...
#> ..$ dev : chr [1:10] "1" "2" "3" "4" ...
Created on 2022-10-26 with reprex v2.0.2
It's an object of S3 class "triangle"
that sub-classes class "matrix"
. You can perform all matrix operations with it. It has a "dimnames"
attribute (not all matrices do) meaning its dimensions have names and you can index entries, rows or columns by numbers or by those names.
When printed it prints like a "matrix"
with the dimnames names set. So, check the print
method for objects of class "triangle"
.
suppressPackageStartupMessages(library(ChainLadder))
getAnywhere("print.triangle")
#> A single object matching 'print.triangle' was found
#> It was found in the following places
#> registered S3 method for print from namespace ChainLadder
#> namespace:ChainLadder
#> with value
#>
#> function (x, ...)
#> {
#> ret <- x
#> class(x) <- tail(class(x), -1)
#> NextMethod(x, ...)
#> invisible(ret)
#> }
#> <bytecode: 0x000001b46d4c4d18>
#> <environment: namespace:ChainLadder>
tail(class(GenIns), -1)
#> [1] "matrix"
Created on 2022-10-26 with reprex v2.0.2
The print
method saves a copy of its 1st argument, then removes the first value of the class
attribute values.
Then calls NextMethod
, meaning, calls the print
method for the 2nd value of the class
attribute, which is "matrix"
. It's this 2nd print
method that does the printing. The function's programmers let another, well tested function print objects of their new class. And invisibly, in order not to print it again, returns the original, saved object.
Upvotes: 0