使用rowSums在R中创建变量

hrirmatl  于 2022-12-25  发布在  其他
关注(0)|答案(1)|浏览(189)

我想在预期变量中记录“是”的发生率。

  1. library(dplyr)
  2. set.seed(2022)
  3. mydata <- tibble::tibble(
  4. "id" = 1:100,
  5. "a1" = sample(c(rep("Yes", 40), rep_len(NA, 100)), 100),
  6. "a2" = sample(c(rep("Yes", 50), rep_len(NA, 100)), 100),
  7. "a3" = sample(c(rep("Yes", 40), rep_len(NA, 100)), 100),
  8. "a4" = sample(c(rep("Yes", 50), rep_len(NA, 100)), 100),
  9. "b2" = rnorm(100, 50, 10)
  10. )
  11. # Goal is to capture any occurrence of Yes in (a* variables)
  12. anymatch <- function(vars){
  13. rowSums(select(cur_data(), all_of(vars))=="Yes")
  14. }
  15. avars <- paste0("a", 1:4)
  16. mydata %>%
  17. mutate(afin = anymatch(avars)) %>%
  18. select(avars, afin)
uqzxnwby

uqzxnwby1#

我们需要na.rm = TRUE

  1. anymatch <- function(vars){
  2. rowSums(select(cur_data(), all_of(vars))=="Yes", na.rm = TRUE)
  3. }

现在它给出了正确的计数

  1. > mydata %>%
  2. mutate(afin = anymatch(avars)) %>%
  3. select(all_of(avars), afin)
  4. # A tibble: 100 × 5
  5. a1 a2 a3 a4 afin
  6. <chr> <chr> <chr> <chr> <dbl>
  7. 1 <NA> <NA> <NA> <NA> 0
  8. 2 <NA> Yes <NA> Yes 2
  9. 3 Yes <NA> <NA> <NA> 1
  10. 4 <NA> Yes Yes <NA> 2
  11. 5 Yes Yes <NA> <NA> 2
  12. 6 Yes Yes Yes Yes 4
  13. 7 <NA> Yes <NA> <NA> 1
  14. 8 <NA> <NA> <NA> <NA> 0
  15. 9 Yes Yes <NA> Yes 3
  16. 10 <NA> Yes <NA> <NA> 1
  17. # … with 90 more rows
  18. # ℹ Use `print(n = ...)` to see more rows

在将来的版本中,我们可能会使用pick而不是cur_data()

  1. anymatch <- function(vars){
  2. rowSums(pick(all_of(vars))=="Yes", na.rm = TRUE)
  3. }
  4. mydata %>%
  5. mutate(afin = anymatch(avars)) %>%
  6. select(all_of(avars), afin)
  7. # A tibble: 100 × 5
  8. a1 a2 a3 a4 afin
  9. <chr> <chr> <chr> <chr> <dbl>
  10. 1 <NA> <NA> <NA> <NA> 0
  11. 2 <NA> Yes <NA> Yes 2
  12. 3 Yes <NA> <NA> <NA> 1
  13. 4 <NA> Yes Yes <NA> 2
  14. 5 Yes Yes <NA> <NA> 2
  15. 6 Yes Yes Yes Yes 4
  16. 7 <NA> Yes <NA> <NA> 1
  17. 8 <NA> <NA> <NA> <NA> 0
  18. 9 Yes Yes <NA> Yes 3
  19. 10 <NA> Yes <NA> <NA> 1
  20. # … with 90 more rows
展开查看全部

相关问题