1
votes

I have a following problem. I have two dataframes (df1 and df2) and I want to calculate a mean of them. I read data from xls, so I show you a print screen of my dataframe, because I don`t know how to name columns "1.", "2." etc.

enter image description here

I use this code:


df_together <- bind_rows(df1 %>% add_rownames(), 
                          df2 %>% add_rownames()) %>% 
  # evaluate following calls for each value in the rowname column
  group_by(rowname) %>% 
  # mean
  summarise_all(mean)

This code worked a year ago, but know I get this error message. I don`t know, what this error means:

<error/rlang_error>
Obsolete data mask.
x Too late to resolve `měsíc` after the end of `dplyr::summarise()`.
i Did you save an object that uses `měsíc` lazily in a column in the `dplyr::summarise()` expression ?
Backtrace:
     x
  1. +-`%>%`(...)
  2. \-dplyr::summarise_all(., mean)
  3.   +-dplyr::summarise(.tbl, !!!funs)
  4.   \-dplyr:::summarise.grouped_df(.tbl, !!!funs)
  5.     \-dplyr:::summarise_cols(.data, ...)
  6.       \-mask$forget("summarise")
  7.         +-base::suppressWarnings(...)
  8.         | \-base::withCallingHandlers(expr, warning = function(w) invokeRestart("muffleWarning"))
  9.         +-rlang::env_bind_lazy(bindings, !!!set_names(promises, names_bindings))
 10.         \-dplyr:::osbolete_promise_fn("mesíc")

How can I fix it please? This did not help me: Obsolete data mask. Too late to resolve `xxxxxx` after the end of `dplyr::mutate()`

1
This is working for me on dplyr 1.0.4` bind_rows(mtcars %>% add_rownames(), mtcars %>% add_rownames()) %>% group_by(rowname) %>% summarise_all(mean) But, there is a recommendation to use tibble::rownames_to_column() and summarise(across(everything(), mean))akrun
@akrun works, thanks!vojtam
Is it the rownames_to_column or summarise(acrossakrun
@akrun its the summarise(across(everything(), mean))`vojtam

1 Answers

2
votes

In the newer versions of dplyr, it is recommended to replace the add_rownames with tibble::rownames_to_column and the summarise_all with summarise(across. Using a reproducible example

library(dplyr)
mtcars %>%
    tibble::rownames_to_column() %>%
    bind_rows(mtcars %>%
        tibble::rownames_to_column()) %>%
    group_by(rowname) %>%
    summarise(across(everything(), mean, na.rm = TRUE), .groups = 'drop')