Overall, what you have looks fine if that's what you want. If you just wanted a single number you could use the filter
and summarize
:
tibble(x = -5:5) %>%
dplyr::filter(x > 0) %>%
dplyr::summarize(mean = mean(x))
# 3
You could also do group_by
if you wanted, but this would give you the average of non-positive values too:
tibble(x = -5:5) %>%
dplyr::group_by(group = x > 0) %>%
dplyr::mutate(mean = mean(x)) %>%
dplyr::ungroup() %>%
dplyr::select(-group)
# A tibble: 11 x 2
x mean
<int> <dbl>
1 -5 -2.5
2 -4 -2.5
3 -3 -2.5
4 -2 -2.5
5 -1 -2.5
6 0 -2.5
7 1 3
8 2 3
9 3 3
10 4 3
11 5 3
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…