【问题标题】:How to count columns that match a condition rowwise?如何按行计算匹配条件的列?
【发布时间】:2021-07-04 15:33:01
【问题描述】:

获取这个data.frame:

demo<-structure(list(q5a = c(1, 1, 10, 10, 8, 7, 8, 8, 2, 10), q5b = c(10, 
6, 10, 7, 5, 10, 8, 8, 8, 8), q5c = c(5, 6, 10, 8, 8, 6, 10, 
10, 1, 9), q5d = c(10, 2, 10, 10, 10, 9, 10, 10, 10, NA), q5e = c(5, 
NA, 10, 10, 8, 9, 10, 10, 1, 8), q5f = c(1, 2, 10, 8, 8, 9, 10, 
10, 1, 6), q5g = c(10, 4, 8, 10, 2, 8, 8, 8, 10, 6), q5h = c(1, 
1, 10, 10, 9, 8, 10, 10, 1, 6), q5i = c(5, 3, 10, 6, 4, 2, 6, 
10, 1, 3), q5j = c(10, 10, 1, 1, 2, 6, 8, 6, 9, 1), q5k = c(10, 
2, 10, 10, 10, 10, 10, 10, 10, 6), q5l = c(10, 5, 10, 10, 9, 
10, 10, 10, 10, 8), q5m = c(6, 4, 10, 10, 9, 10, 10, 10, 9, 8
), q5n = c(10, 1, 10, 10, 5, 10, 10, 8, 10, 9), q5o = c(10, 4, 
10, 10, 5, 10, 8, 8, 10, 8)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

我想创建一个汇总变量,告诉我每个 有多少个变量等于 10。

更好的是,如果这些列是更大数据框(q1、q2、q3 等)的一部分,我如何创建一个新变量,该变量仅采用那些以 q5 开头的列并在他们? 我更喜欢使用 tidyverse,但也欢迎使用基本解决方案。

所需的最终结果应如下所示:

# A tibble: 10 x 15
     q5a   q5b   q5c   q5d   q5e   q5f   q5g   q5h   q5i   q5j   q5k   q5l   q5m   q5n   q5o tens
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1     1    10     5    10     5     1    10     1     5    10    10    10     6    10    10   8
 2     1     6     6     2    NA     2     4     1     3    10     2     5     4     1     4   1
 3    10    10    10    10    10    10     8    10    10     1    10    10    10    10    10  13
 4    10     7     8    10    10     8    10    10     6     1    10    10    10    10    10  10

等等……

我觉得这应该很容易,但是自从我研究 R 以来已经有一段时间了,我只是想不通。 谢谢!

【问题讨论】:

    标签: r


    【解决方案1】:

    一个选项可能是:

    demo %>%
     mutate(tems = rowSums(select(., starts_with("q5")) == 10, na.rm = TRUE))
    
         q5a   q5b   q5c   q5d   q5e   q5f   q5g   q5h   q5i   q5j   q5k   q5l   q5m   q5n   q5o  tems
       <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
     1     1    10     5    10     5     1    10     1     5    10    10    10     6    10    10     8
     2     1     6     6     2    NA     2     4     1     3    10     2     5     4     1     4     1
     3    10    10    10    10    10    10     8    10    10     1    10    10    10    10    10    13
    

    【讨论】:

    • 行总和!这就是我试图记住的。谢谢!我会尽快接受这个解决方案。
    【解决方案2】:

    我们可以在rowwise之后使用sumc_across

    library(dplyr)
    demo %>%
       rowwise %>% 
       mutate(tems = sum(c_across(starts_with('q5')) == 10, na.rm = TRUE)) %>%
       ungroup
    

    -输出

    # A tibble: 10 x 16
    #     q5a   q5b   q5c   q5d   q5e   q5f   q5g   q5h   q5i   q5j   q5k   q5l   q5m   q5n   q5o  tems
    #   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
    # 1     1    10     5    10     5     1    10     1     5    10    10    10     6    10    10     8
    # 2     1     6     6     2    NA     2     4     1     3    10     2     5     4     1     4     1
    # 3    10    10    10    10    10    10     8    10    10     1    10    10    10    10    10    13
    # ...
    

    或者使用collapse

    library(collapse)
    demo$tems <- dapply(gvr(demo, '^q5'),  function(x)
            fsum(x == 10), MARGIN = 1)
    demo$tems
    #[1]  8  1 13 10  2  6  9  9  6  1
    

    【讨论】:

      【解决方案3】:

      data.table 选项

      setDT(demo)[, tens := rowSums(.SD == 10, na.rm = TRUE), .SDcols = patterns("^q5")]
      

      给予

          q5a q5b q5c q5d q5e q5f q5g q5h q5i q5j q5k q5l q5m q5n q5o tens
       1:   1  10   5  10   5   1  10   1   5  10  10  10   6  10  10    8
       2:   1   6   6   2  NA   2   4   1   3  10   2   5   4   1   4    1
       3:  10  10  10  10  10  10   8  10  10   1  10  10  10  10  10   13
       4:  10   7   8  10  10   8  10  10   6   1  10  10  10  10  10   10
       5:   8   5   8  10   8   8   2   9   4   2  10   9   9   5   5    2
       6:   7  10   6   9   9   9   8   8   2   6  10  10  10  10  10    6
       7:   8   8  10  10  10  10   8  10   6   8  10  10  10  10   8    9
       8:   8   8  10  10  10  10   8  10  10   6  10  10  10   8   8    9
       9:   2   8   1  10   1   1  10   1   1   9  10  10   9  10  10    6
      10:  10   8   9  NA   8   6   6   6   3   1   6   8   8   9   8    1
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-24
        • 2021-06-15
        • 1970-01-01
        • 2022-11-10
        • 2021-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多