[R] transform a list of arrays to tibble

arnaud gaboury @rn@ud@g@boury @end|ng |rom gm@||@com
Tue Oct 17 15:48:13 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)

asset.stats <- structure(list(Assets = c("BTCUSDT", "ETHUSDT",
"TRXUSDT"), Annualized_return = c(BTCUSDT = 15.36,
ETHUSDT = 4.06, TRXUSDT = 10.9)), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame"))

On Tue, Oct 17, 2023 at 3:33 PM <avi.e.gross using gmail.com> wrote:
>
> Arnaud,
>
>
> Short answer may be that the tibble data structure will not be supporting row names and you may want to simply save those names in an additional column or externally.
>
> My first thought was to simply save the names you need and then put them back on the tibble. In your code, something like this:
>
> save.names <- names(my.ret.lst)
> result.tib <- as_tibble_col(unlist(my.ret.lst), column_name = 'return')
> rownames(result.tib) <- save.names
>
> Unfortunately, I got an error message:
>
> > save.names
> [1] "BTCUSDT" "ETHUSDT" "TRXUSDT"
> > rownames(result.tib) <- save.names
> Warning message:
> Setting row names on a tibble is deprecated.
> Error in exists(cacheKey, where = .rs.WorkingDataEnv, inherits = FALSE) :
>   invalid first argument
> Error in assign(cacheKey, frame, .rs.CachedDataEnv) :
>   attempt to use zero-length variable name
>
> If a tibble deprecates row names, it may not be the ideal storage for you. A plain data.frame works:
>
> > result.df <- as.data.frame(result.tib)
> > rownames(result.df) <- save.names
> > result.df
>         return
> BTCUSDT  15.36
> ETHUSDT   4.06
> TRXUSDT  10.90
>
> Trying to convert it to a tibble, as anticipated, is not working for me:
>
> > as.tibble(result.df)
> # A tibble: 3 × 1
>   return
>    <dbl>
> 1  15.4
> 2   4.06
> 3  10.9
> Warning message:
> `as.tibble()` was deprecated in tibble 2.0.0.
> ℹ Please use `as_tibble()` instead.
> ℹ The signature and semantics have changed, see `?as_tibble`.
> This warning is displayed once every 8 hours.
> Call `lifecycle::last_lifecycle_warnings()` to see where this
> warning was generated.
>
> You can, instead, create a matrix and assign the row and column names you save or create:
>
> result.mat <- matrix(my.ret.lst)
> colnames(result.mat) <- c("return")
> rownames(result.mat) <- save.names
>
> > result.mat
>         return
> BTCUSDT 15.36
> ETHUSDT 4.06
> TRXUSDT 10.9
>
> But saving a matrix to reuse has other considerations.
>
> So, if I may make a suggestion, if you really want a tibble that allows you to know what each row is for, consider one of many methods for saving the previous row names as a new column. I used that to take the data.frame version I made above and got:
>
> > temp <- as_tibble(result.df, rownames="rows")
> > temp
> # A tibble: 3 × 2
>   rows    return
>   <chr>    <dbl>
> 1 BTCUSDT  15.4
> 2 ETHUSDT   4.06
> 3 TRXUSDT  10.9
>
> Note the above uses as_tibble with an underscore, but many other ways to make a column exist.
>
>
> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of arnaud gaboury
> Sent: Tuesday, October 17, 2023 4:30 AM
> To: r-help <r-help using r-project.org>
> Subject: [R] transform a list of arrays to tibble
>
> 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
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list