0

我有以下名为“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   
4

1 回答 1

1

试试这个:

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
于 2020-11-20T13:50:52.707 回答