[R-SIG-Finance] from a list of array to tibble

arnaud gaboury @rn@ud@g@boury @end|ng |rom gm@||@com
Tue Oct 17 15:43:49 CEST 2023


In fact, I realized this is not recommended to give names to rows. A
better approach is to add a column with all names as row. The
following does the job:

asset.stats <- as_tibble_col(unlist(my.ret.lst), column_name =
'Annualized_return')
asset.stats <- rownames_to_column(asset.stats, var = 'Assets')
asset.stats$Assets <- names(my.ret.lst)

On Tue, Oct 17, 2023 at 2:52 PM Adam Ginensky <adamno227 using gmail.com> wrote:
>
> Perhaps you should have used 'The trouble with tibbles" as your subject line :)
>
> On Tue, Oct 17, 2023 at 6:21 AM Enrico Schumann <es using enricoschumann.net> wrote:
>>
>> On Mon, 16 Oct 2023, arnaud gaboury writes:
>>
>> > I work with a list of crypto assets daily closing prices in 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 a way to start my tibble. The idea is to have variables
>> > BTCUSDT, ETHUSDT and TRXUSDT (token names), with observations (like
>> > annualized return) and the respective values. I will add of course many
>> > other observations.My row names can be either my token names, or the
>> > observation (annualized return).
>> > In my example, I can't find a way to pass each list element name to the
>> > wanted tibble.
>> >
>> > Thank you for help
>> >
>>
>> Do you mean something like this?
>>
>>     data.frame(my.ret.lst)
>>     ##                   BTCUSDT ETHUSDT TRXUSDT
>>     ## Annualized Return   15.36    4.06    10.9

this is the idea, but BTCUSDT etc as rows and Annualized_Return as one
column name.

>>
>> (I don't use tibbles, but there is probably an 'as.tibble' method or
>>  similar.)
>>
>> --
>> Enrico Schumann
>> Lucerne, Switzerland
>> http://enricoschumann.net
>>
>> _______________________________________________
>> R-SIG-Finance using r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> -- Subscriber-posting only. If you want to post, subscribe first.
>> -- Also note that this is not the r-help list where general R questions should go.



More information about the R-SIG-Finance mailing list