[R] transform a list of arrays to tibble

arnaud gaboury @rn@ud@g@boury @end|ng |rom gm@||@com
Tue Oct 17 10:29:33 CEST 2023


I work with a list of crypto assets daily closing prices in a xts
class. Here is a limited example:

asset.xts.lst <- list(BTCUSDT = structure(c(26759.63, 26862, 26852.48, 27154.15,
27973.45), dim = c(5L, 1L), index = structure(c(1697068800, 1697155200,
1697241600, 1697328000, 1697414400), tzone = "UTC", tclass = "Date"),
class = c("xts",
"zoo")), ETHUSDT = structure(c(1539.61, 1552.16, 1554.94, 1557.77,
1579.73), dim = c(5L, 1L), index = structure(c(1697068800, 1697155200,
1697241600, 1697328000, 1697414400), tzone = "UTC", tclass = "Date"),
class = c("xts",
"zoo")), TRXUSDT = structure(c(0.08481, 0.08549, 0.08501, 0.08667,
0.08821), dim = c(5L, 1L), index = structure(c(1697068800, 1697155200,
1697241600, 1697328000, 1697414400), tzone = "UTC", tclass = "Date"),
class = c("xts",
"zoo")))

I will compute some function from PerformanceAnalytics package and
write all results in a tibble. Let's apply a first function,
Return.annualized() (at first I computed returns from daily prices). I
have now a list of arrays named my.ret.lst:

my.ret.lst <- list(BTCUSDT = structure(15.36, dim = c(1L, 1L), dimnames = list(
    "Annualized Return", NULL)), ETHUSDT = structure(4.06, dim = c(1L,
1L), dimnames = list("Annualized Return", NULL)), TRXUSDT =
structure(10.9, dim = c(1L,
1L), dimnames = list("Annualized Return", NULL)))

Now I can't find how to build a  tibble in a specific format (asset
names as row names and observations as column names) .
I can of course run:
> mytb <- as_tibble(unlist(my.ret.lst)
but I loose row and column names.
> as_tibble_col(unlist(my.ret.lst), column_name = 'return')
will give me the wanted column name but row names (in my case asset
names) are missing.


Thank you for help



More information about the R-help mailing list