Home > Net >  Keep names from quantile() function when used in a data.table
Keep names from quantile() function when used in a data.table

Time:10-04

I want to keep the named vector produce by say a function like quantile() where I want to have a column called say name that is the value of the probs passed.

Example that gives me what I want using tidyverse:

library(TidyDensity)
library(data.table)
library(reshape)

func <- quantile

x <- mtcars$mpg

tb <- tidy_bootstrap(x) %>%
  bootstrap_unnest_tbl()

df_tbl <- tb %>% 
  split(.$sim_number) %>%
  map(~ .x %>% pull(y))

purrr::map(
    df_tbl, ~ func(.x) %>%
      purrr::imap(.f = ~ cbind(.x, name = .y)) %>%
      purrr::map_df(dplyr::as_tibble)
  ) %>%
    purrr::imap(.f = ~ cbind(.x, sim_number = .y)) %>%
    purrr::map_df(dplyr::as_tibble) %>%
    dplyr::select(sim_number, name, .x) %>%
    dplyr::mutate(.x = as.numeric(.x)) %>%
    dplyr::mutate(sim_number = factor(sim_number)) %>%
    dplyr::rename(value = .x)

# A tibble: 10,000 × 3
   sim_number name  value
   <fct>      <chr> <dbl>
 1 1          0%     10.4
 2 1          25%    14.7
 3 1          50%    19.2
 4 1          75%    22.8
 5 1          100%   33.9
 6 2          0%     10.4
 7 2          25%    14.7
 8 2          50%    17.8
 9 2          75%    21.4
10 2          100%   32.4
# … with 9,990 more rows

When I use data.table I do not know how to make a column of the names of the vector that gets returned from the quantile() function, and this could come from any function really.

Here is what I get:

tbd <-  tb %>%
  as.data.table()

> tbd[, .(quantile = func(y)), by = sim_number]
       sim_number quantile
    1:          1     10.4
    2:          1     14.7
    3:          1     19.2
    4:          1     22.8
    5:          1     33.9
   ---                    
 9996:       2000     10.4
 9997:       2000     15.5
 9998:       2000     17.8
 9999:       2000     21.4
10000:       2000     32.4

CodePudding user response:

We could melt

melt(tbd[, as.list(func(y)), sim_number], id.var = 'sim_number',
        value.name = 'quantile')

-output

      sim_number variable quantile
           <fctr>   <fctr>    <num>
    1:          1       0%     14.7
    2:          2       0%     10.4
    3:          3       0%     10.4
    4:          4       0%     10.4
    5:          5       0%     10.4
   ---                             
 9996:       1996     100%     30.4
 9997:       1997     100%     33.9
 9998:       1998     100%     33.9
 9999:       1999     100%     33.9
10000:       2000     100%     33.9
  • Related