【问题标题】:Counting non-NA values per row including certain columns in R计算每行的非 NA 值,包括 R 中的某些列
【发布时间】:2022-07-15 20:17:08
【问题描述】:

如何计算每行的非 NA 值,包括从“b”到“c”的列?

library(tidyverse)

d = tibble(a = c("Tom", "Mary", "Ben", "Jane", "Lucas", "Mark"),
           b = c(NA, 3, 6, NA, 5, NA),
           c = c(2, NA, 6, 7, 1, 9))

d

输出应该有一个额外的列,其值如下:1、1、2、1、2、1

特别感谢 Tidyverse 解决方案!

【问题讨论】:

    标签: r tidyverse data-manipulation


    【解决方案1】:

    一个可能的解决方案:

    d %>% 
      mutate( result = rowSums(!is.na(across(b:c))))
    

    【讨论】:

      猜你喜欢
      • 2020-03-07
      • 2012-10-12
      • 2019-08-28
      • 2019-08-04
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      • 2016-10-02
      • 2019-04-03
      相关资源
      最近更新 更多