Skip to contents

Default behaviors:

  • Always includes rownames in tibble, if available

  • Does not repeat input elements to obtain common lengths

  • Arranges lists with uniform arrays into columns of a single tibble

  • A list of ragged elements becomes a two column tibble where the first column contains names and the second column is a nested list

  • Is unconcerned with duplicate column names unless specified by user with .name_repair

Usage

entibble(
  ...,
  .rowname = "rowname",
  .name_repair = c("minimal", "check_unique", "unique", "universal", "unique_quiet",
    "universal_quiet")
)

Arguments

...

object or expression to convert to a tibble

.rowname

string; to name the column containing rownames. If rownames are not present in the dataframe, .rowname is ignored.

.name_repair

Treatment of problematic column names:

  • "minimal": No name repair or checks, beyond basic existence,

  • "unique": Make sure names are unique and not empty,

  • "check_unique": (default value), no name repair, but check they are unique,

  • "universal": Make the names unique and syntactic

  • a function: apply custom name repair (e.g., .name_repair = make.names for names in the style of base R).

  • A purrr-style anonymous function, see rlang::as_function()

This argument is passed on as repair to vctrs::vec_as_names(). See there for more details on these terms and the strategies used to enforce them.

Value

a tibble with rownames in a column

Details

@details Built to work with the function xl() to produce 2D output for spreadsheets. entibble() is liberal about names, such as duplicate column names, and tries to produce flat 2D data in place of nested data.

See also

Examples

letters |> entibble()
#> # A tibble: 26 × 1
#>    letters
#>    <chr>  
#>  1 a      
#>  2 b      
#>  3 c      
#>  4 d      
#>  5 e      
#>  6 f      
#>  7 g      
#>  8 h      
#>  9 i      
#> 10 j      
#> # ℹ 16 more rows

# naming examples
(example_data <- letters |>  purrr::set_names(LETTERS))
#>   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T 
#> "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" 
#>   U   V   W   X   Y   Z 
#> "u" "v" "w" "x" "y" "z" 
example_data |> entibble()
#> # A tibble: 26 × 2
#>    rowname example_data
#>    <chr>   <chr>       
#>  1 A       a           
#>  2 B       b           
#>  3 C       c           
#>  4 D       d           
#>  5 E       e           
#>  6 F       f           
#>  7 G       g           
#>  8 H       h           
#>  9 I       i           
#> 10 J       j           
#> # ℹ 16 more rows
example_data |> entibble(.rowname = 'a user-specified name')
#> # A tibble: 26 × 2
#>    `a user-specified name` example_data
#>    <chr>                   <chr>       
#>  1 A                       a           
#>  2 B                       b           
#>  3 C                       c           
#>  4 D                       d           
#>  5 E                       e           
#>  6 F                       f           
#>  7 G                       g           
#>  8 H                       h           
#>  9 I                       i           
#> 10 J                       j           
#> # ℹ 16 more rows
example_data |> entibble(.name_repair = ~c('name_A', 'name_B'))
#> # A tibble: 26 × 2
#>    name_A name_B
#>    <chr>  <chr> 
#>  1 A      a     
#>  2 B      b     
#>  3 C      c     
#>  4 D      d     
#>  5 E      e     
#>  6 F      f     
#>  7 G      g     
#>  8 H      h     
#>  9 I      i     
#> 10 J      j     
#> # ℹ 16 more rows
entibble(example_data, example_data)
#> # A tibble: 26 × 4
#>    rowname example_data rowname example_data
#>    <chr>   <chr>        <chr>   <chr>       
#>  1 A       a            A       a           
#>  2 B       b            B       b           
#>  3 C       c            C       c           
#>  4 D       d            D       d           
#>  5 E       e            E       e           
#>  6 F       f            F       f           
#>  7 G       g            G       g           
#>  8 H       h            H       h           
#>  9 I       i            I       i           
#> 10 J       j            J       j           
#> # ℹ 16 more rows
entibble(x = 1:3, x = 11:13, .name_repair = 'unique_quiet')
#> # A tibble: 3 × 2
#>   x...1 x...2
#>   <int> <int>
#> 1     1    11
#> 2     2    12
#> 3     3    13
mtcars |> entibble(.rowname = 'vehicle')
#> # A tibble: 32 × 12
#>    vehicle       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>    <chr>       <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1 Mazda RX4    21       6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2 Mazda RX4 …  21       6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3 Datsun 710   22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4 Hornet 4 D…  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  5 Hornet Spo…  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
#>  6 Valiant      18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  7 Duster 360   14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
#>  8 Merc 240D    24.4     4  147.    62  3.69  3.19  20       1     0     4     2
#>  9 Merc 230     22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> 10 Merc 280     19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
#> # ℹ 22 more rows

# nests ragged data
entibble(example_data, !!!letters)
#> # A tibble: 27 × 2
#>    rowname      `list(ragged_elements)`
#>    <chr>        <list>                 
#>  1 example_data <tibble [26 × 2]>      
#>  2 a            <tibble [1 × 1]>       
#>  3 b            <tibble [1 × 1]>       
#>  4 c            <tibble [1 × 1]>       
#>  5 d            <tibble [1 × 1]>       
#>  6 e            <tibble [1 × 1]>       
#>  7 f            <tibble [1 × 1]>       
#>  8 g            <tibble [1 × 1]>       
#>  9 h            <tibble [1 × 1]>       
#> 10 i            <tibble [1 × 1]>       
#> # ℹ 17 more rows

# spreads conforming rows and lists of conforming rows
entibble(x = 1:3, y = 11:13)
#> # A tibble: 3 × 2
#>       x     y
#>   <int> <int>
#> 1     1    11
#> 2     2    12
#> 3     3    13
entibble(x = 1:3, y = 10:14)
#> # A tibble: 2 × 2
#>   rowname `list(ragged_elements)`
#>   <chr>   <list>                 
#> 1 x       <tibble [3 × 1]>       
#> 2 y       <tibble [5 × 1]>       

# be somewhat careful about conforming-row inputs; it is different from `tibble()`
enlist(head(iris), tail(mtcars)) |> entibble()
#> # A tibble: 6 × 17
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species rowname    mpg   cyl
#>          <dbl>       <dbl>        <dbl>       <dbl> <chr>   <chr>    <dbl> <dbl>
#> 1          5.1         3.5          1.4         0.2 setosa  Porsche…  26       4
#> 2          4.9         3            1.4         0.2 setosa  Lotus E…  30.4     4
#> 3          4.7         3.2          1.3         0.2 setosa  Ford Pa…  15.8     8
#> 4          4.6         3.1          1.5         0.2 setosa  Ferrari…  19.7     6
#> 5          5           3.6          1.4         0.2 setosa  Maserat…  15       8
#> 6          5.4         3.9          1.7         0.4 setosa  Volvo 1…  21.4     4
#> # ℹ 9 more variables: disp <dbl>, hp <dbl>, drat <dbl>, wt <dbl>, qsec <dbl>,
#> #   vs <dbl>, am <dbl>, gear <dbl>, carb <dbl>
as.matrix(list(1:10,11:20)) |> entibble()
#> # A tibble: 10 × 2
#>      `1`   `2`
#>    <int> <int>
#>  1     1    11
#>  2     2    12
#>  3     3    13
#>  4     4    14
#>  5     5    15
#>  6     6    16
#>  7     7    17
#>  8     8    18
#>  9     9    19
#> 10    10    20

# if you want to ensure separate lists, call [purrr::map()] or [list_iron()]
enlist(head(iris), tail(mtcars)) |> purrr::map(entibble)
#> $`head(iris)`
#> # A tibble: 6 × 5
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>          <dbl>       <dbl>        <dbl>       <dbl> <chr>  
#> 1          5.1         3.5          1.4         0.2 setosa 
#> 2          4.9         3            1.4         0.2 setosa 
#> 3          4.7         3.2          1.3         0.2 setosa 
#> 4          4.6         3.1          1.5         0.2 setosa 
#> 5          5           3.6          1.4         0.2 setosa 
#> 6          5.4         3.9          1.7         0.4 setosa 
#> 
#> $`tail(mtcars)`
#> # A tibble: 6 × 12
#>   rowname        mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>   <chr>        <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 Porsche 914…  26       4 120.     91  4.43  2.14  16.7     0     1     5     2
#> 2 Lotus Europa  30.4     4  95.1   113  3.77  1.51  16.9     1     1     5     2
#> 3 Ford Panter…  15.8     8 351     264  4.22  3.17  14.5     0     1     5     4
#> 4 Ferrari Dino  19.7     6 145     175  3.62  2.77  15.5     0     1     5     6
#> 5 Maserati Bo…  15       8 301     335  3.54  3.57  14.6     0     1     5     8
#> 6 Volvo 142E    21.4     4 121     109  4.11  2.78  18.6     1     1     4     2
#> 
enlist(head(iris), tail(mtcars)) |> list_iron(.f = entibble)
#> $`enlist(head(iris), tail(mtcars))|head(iris)`
#> # A tibble: 6 × 5
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>          <dbl>       <dbl>        <dbl>       <dbl> <chr>  
#> 1          5.1         3.5          1.4         0.2 setosa 
#> 2          4.9         3            1.4         0.2 setosa 
#> 3          4.7         3.2          1.3         0.2 setosa 
#> 4          4.6         3.1          1.5         0.2 setosa 
#> 5          5           3.6          1.4         0.2 setosa 
#> 6          5.4         3.9          1.7         0.4 setosa 
#> 
#> $`enlist(head(iris), tail(mtcars))|tail(mtcars)`
#> # A tibble: 6 × 12
#>   rowname        mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>   <chr>        <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 Porsche 914…  26       4 120.     91  4.43  2.14  16.7     0     1     5     2
#> 2 Lotus Europa  30.4     4  95.1   113  3.77  1.51  16.9     1     1     5     2
#> 3 Ford Panter…  15.8     8 351     264  4.22  3.17  14.5     0     1     5     4
#> 4 Ferrari Dino  19.7     6 145     175  3.62  2.77  15.5     0     1     5     6
#> 5 Maserati Bo…  15       8 301     335  3.54  3.57  14.6     0     1     5     8
#> 6 Volvo 142E    21.4     4 121     109  4.11  2.78  18.6     1     1     4     2
#> 

# makes multi-dimensional tables 2D:
Titanic |> dim()
#> [1] 4 2 2 2
Titanic |> entibble()
#> # A tibble: 32 × 5
#>    Class Sex    Age   Survived  Freq
#>    <chr> <chr>  <chr> <chr>    <dbl>
#>  1 1st   Male   Child No           0
#>  2 2nd   Male   Child No           0
#>  3 3rd   Male   Child No          35
#>  4 Crew  Male   Child No           0
#>  5 1st   Female Child No           0
#>  6 2nd   Female Child No           0
#>  7 3rd   Female Child No          17
#>  8 Crew  Female Child No           0
#>  9 1st   Male   Adult No         118
#> 10 2nd   Male   Adult No         154
#> # ℹ 22 more rows