R's new native pipe does not work How to solve?

58 views Asked by At

This is my reproducible sample data

df1 <- structure(list(No = 1:3, A = c(1L, 0L, 0L), B = c(0L, 1L, 0L),
                      C = c(1L, 2L, 1L), D = c(0L, 1L, 0L)), class = "data.frame", row.names = c(NA, -3L))

df2 <- structure(list(No = 1:3, A = c(1L, 0L, 2L), B = c(0L, 1L, 1L), E = c(1L, 2L, 1L), 
                      F = c(1L, 1L, 0L)), class = "data.frame", row.names = c(NA, -3L))

I want to merge these two dfs and then compute the sum of each column while group_by the No column.

With the old native pipe, it worked well.

library(tidyverse)
bind_rows(df1, df2) %>%
  group_by(No) %>%
  summarise(across(colnames(.)[-1], sum, na.rm = TRUE), .groups = "drop")

# A tibble: 3 x 7
     No     A     B     C     D     E     F
  <int> <int> <int> <int> <int> <int> <int>
1     1     2     0     1     0     1     1
2     2     0     2     2     1     2     1
3     3     2     1     1     0     1     0

However, if I use the new native pipe |>, I got this error

Error in `summarise()`:
i In argument: `across(colnames(.)[-1], sum, na.rm = TRUE)`.
i In group 1: `No = 1`.
Caused by error in `across()`:
! Problem while evaluating `colnames(.)[-1]`.
Caused by error in `is.data.frame()`:
! object '.' not found

I realized that there are some limitations using the new native pipe. Any suggestions for this case?

0

There are 0 answers