How to replace vector tidier

Viewed 1373

I am looking to find another function that can replace the broom::tidy() function after it get removed. Here is what the broom package warning says:

Tidy Atomic Vectors Vector tidiers are deprecated and will be removed from an upcoming release of broom.

Here is a description of function:

tidy() produces a tibble() where each row contains information about an important component of the model. For regression models, this often corresponds to regression coefficients. This is can be useful if you want to inspect a model or create custom visualizations.

Thanks you,

John

3 Answers

As I understand the warning, there is no general deprecation of the function broom::tidy, this warning only occurs when it is called with an atomic vector. In this case tibble() seems to be a slot-in replacement:

No deprecation warning for tidy() when called for a linear model:

library(broom)
fit <- lm(Volume ~ Girth + Height, trees)
tidy(fit)
## A tibble: 3 x 5
#  term        estimate std.error statistic  p.value
#  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
#1 (Intercept)  -58.0       8.64      -6.71 2.75e- 7
#2 Girth          4.71      0.264     17.8  8.22e-17
#3 Height         0.339     0.130      2.61 1.45e- 2

#Deprecation warning:

tidy(1:5)
## A tibble: 5 x 1
#      x
#  <int>
#1     1
#2     2
#3     3
#4     4
#5     5
#Warning messages:
#1: 'tidy.numeric' is deprecated.
#See help("Deprecated") 
#2: `data_frame()` is deprecated as of tibble 1.1.0.
#Please use `tibble()` instead.

No warning for tibble, same output :

tibble(1:5)
## A tibble: 5 x 1
#  `1:5`
#  <int>
#1     1
#2     2
#3     3
#4     4
#5     5

The deprecation warning is letting you know that the method tidy.numeric is being removed.

broom:::tidy.numeric
function (x, ...) 
{
    .Deprecated()
    if (!is.null(names(x))) {
        dplyr::data_frame(names = names(x), x = unname(x))
    }
    else {
        dplyr::data_frame(x = x)
    }
}

You can see the call to .Deprecated there, and the rest of the function just calls data_frame. As this function is also being deprecated, tibble is the new solution. As tibble does not honour row names, if you want to save the names, you could create something similar to the above.

tidy.numeric <- function (x, ...) 
{
    if (!is.null(names(x))) {
        tibble::tibble(names = names(x), x = unname(x))
    }
    else {
        tibble::tibble(x = x)
    }
}

If you try to convert a named vector as mentioned by @Miff, you can also use the function enframe(). It creates a tibble with two columns, one with the names in the vector and one column with the values.

Related