有没有一种方法可以使用rowSums和is.numeric仅对数字列求和?

时间:2020-05-13 22:59:53

标签: r dplyr mutate

一个简短的问题,希望能有一个快速的答案。

我试图仅在具有数字数据的列上使用rowSums。我想做与此等效的事情(使用内置数据集CO2作为可重现的示例):

# Reproducible example  
CO2 %>%
  mutate( Total = rowSums(.[c(-1, -2, -3)]) ) %>% 
  head()

  Plant   Type  Treatment conc uptake Total
1   Qn1 Quebec nonchilled   95   16.0 111.0
2   Qn1 Quebec nonchilled  175   30.4 205.4
3   Qn1 Quebec nonchilled  250   34.8 284.8
4   Qn1 Quebec nonchilled  350   37.2 387.2
5   Qn1 Quebec nonchilled  500   35.3 535.3
6   Qn1 Quebec nonchilled  675   39.2 714.2

我尝试使用mutate和is.numeric的rowSums,但是没有成功。有没有简单的方法可以做到这一点?

# How do sum rows using is.numeric (below does not work)?                 
CO2 %>%
  mutate( Total = rowSums(., is.numeric(.)) )

2 个答案:

答案 0 :(得分:5)

我们可以将select_ifrowSums一起使用

library(dplyr)
CO2 %>% 
    mutate(Total = rowSums(select_if(., is.numeric), na.rm = TRUE)) %>%
    head
#  Plant   Type  Treatment conc uptake Total
#1   Qn1 Quebec nonchilled   95   16.0 111.0
#2   Qn1 Quebec nonchilled  175   30.4 205.4
#3   Qn1 Quebec nonchilled  250   34.8 284.8
#4   Qn1 Quebec nonchilled  350   37.2 387.2
#5   Qn1 Quebec nonchilled  500   35.3 535.3
#6   Qn1 Quebec nonchilled  675   39.2 714.2

答案 1 :(得分:0)

select_if解决方案很棒!如果您想快速求和而不是求和,还可以使用pmap_dbl对其进行扩展。

library(tidyverse) # install.packages("tidyverse")

CO2 <- tribble(
  ~Plant,    ~Type,   ~Treatment, ~conc, ~uptake,
   "Qn1", "Quebec", "nonchilled",   95L,      16,
   "Qn1", "Quebec", "nonchilled",  175L,    30.4,
   "Qn1", "Quebec", "nonchilled",  250L,    34.8,
   "Qn1", "Quebec", "nonchilled",  350L,    37.2,
   "Qn1", "Quebec", "nonchilled",  500L,    35.3,
   "Qn1", "Quebec", "nonchilled",  675L,    39.2
  )

# creating a row-sum column
CO2 %>%
  mutate(total = pmap_dbl(select_if(., is.numeric), sum, na.rm = TRUE))

# alternatively, creating a row-minimum column
CO2 %>%
  mutate(min_val = pmap_dbl(select_if(., is.numeric), min, na.rm = TRUE))