Guberney
Guberney

Reputation: 13

`summarise_each_()` is deprecated as of dplyr 0.7.0. Please use `across()` instead. Problem with `summarise()` input `..1`

I have this and it works:

data %>%
        select_if(function(x) any(is.na(x))) %>% 
        summarise_each(funs(sum(is.na(.))))

Warning message: summarise_each() is deprecated as of dplyr 0.7.0. Please use across() instead.

When I change sumarise_each():

data %>%
        select_if(function(x) any(is.na(x))) %>% 
        summarise(across(funs(sum(is.na(.)))))

This happen:

Error: Problem with summarise() input ..1.
x Must subset columns with a valid subscript vector.
x Subscript has the wrong type fun_list.
ℹ It must be numeric or character.
ℹ Input ..1 is across(funs(sum(is.na(.)))).

I want to update my code with the across function.

Upvotes: 1

Views: 4661

Answers (2)

RStudious
RStudious

Reputation: 166

Here's what I think you're trying to do

    library(dplyr)
    df <- data.frame(id=c("001","002"), A = c(0,0), B= c(NA,0), C=c(NA,1))
    df

         id  A  B  C
1        001 0 NA NA
2        002 0  0  1


    df %>%
    select(where(function(x) any(is.na(x)))) %>%
    rowwise %>%
    mutate(sumNA = sum(is.na(c_across(everything()))))

    # A tibble: 2 x 3
    # Rowwise: 
       B      C    sumNA
     <dbl>  <dbl>  <int>
  1    NA    NA     2
  2    0     1      0

This selects all columns that contain NA and then adds a new column which shows how many NA's are in each row

Upvotes: 0

Ben Norris
Ben Norris

Reputation: 5747

The across way to do this is:

library(dplyr)
df %>%
  summarize(across(where(~any(is.na(.)), ~sum(is.na(.)))) 

starwars %>% 
  summarise(across(where(~any(is.na(.))), ~sum(is.na(.))))

 height  mass hair_color birth_year   sex gender homeworld species
   <int> <int>      <int>      <int> <int>  <int>     <int>   <int>
1      6    28          5         44     4      4        10       4

You no longer need select_if with across(). The where() inside across() allows you do do a logical check. Then you provide the summarizing function. The ~ notation allows you to do a function that refers back to the data, where the dot refers to the data in eahc column.

Upvotes: 1

Related Questions