在 R 中总结(跨越(哪里))

Summarise(across(where)) in R

我有以下名为“data_2”的数据文件:

Person Weight Height
A      55     155
B      65     165
C      75     175

我想使用 Summarise(across(where)) 命令生成总重量和每个人的重量。这是我迄今为止尝试过的方法。

data_2 <- read_excel("data_2.xlsx", sheet = 2)


data_2 %>%
summarise(across(where(is.numeric), sum))

不幸的是,这不能正常工作。有人知道如何解决这个问题吗?

预期输出:

Person Weight 
A      55     
B      65     
C      75
Total  195   

试试这个:

library(dplyr)
#Code
newdf <- data_2 %>%
  bind_rows(data_2 %>% select(-1) %>%
              summarise_all(sum) %>% mutate(Person='Total'))

输出:

  Person Weight Height
1      A     55    155
2      B     65    165
3      C     75    175
4  Total    195    495

或使用您的代码:

#Code 2
newdf <- data_2 %>% 
  bind_rows(data_2 %>% summarise(across(where(is.numeric),sum)) %>%
              mutate(Person='Total')) %>% select(-Height)

输出:

  Person Weight
1      A     55
2      B     65
3      C     75
4  Total    195