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 dplyr. 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
CodePudding user response:
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
CodePudding user response:
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