简体   繁体   中英

tidyverse: finding number of observations greater than multiple values

I want to compute the no. of observations greater than 5 and 7 . This can be achieved by making dummy variables using case_when function from . Would like to know more efficient approach without creating dummy variables.

library(tidyverse)

dt1 <-  tibble(X = 1:10)

dt1 %>% 
  mutate(
    X1 = case_when(X >= 5 ~ 1, X < 5 ~ 0)
  , X2 = case_when(X >= 7 ~ 1, X < 7 ~ 0)
    ) %>% 
  summarise(across(.cols = c(X1, X2), .fns = sum))
#> # A tibble: 1 x 2
#>      X1    X2
#>   <dbl> <dbl>

#> 1     6     4

You can use summarize directly:

dt1 %>% 
  summarise(X1 = sum(X >= 5),
            X2 = sum(X >= 7))

# A tibble: 1 x 2
     X1    X2
  <int> <int>
1     6     4

If there are multiple elements, an option is also to loop with map

library(purrr)
library(dplyr)
library(stringr)
imap_dfc(setNames(c(5, 7), str_c("X", 1:2)), ~ dt1 %>%
      summarise(!! .y := sum(X >= .x)))
# A tibble: 1 × 2
     X1    X2
  <int> <int>
1     6     4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM