Sample data:
df <- tibble(x = c(0.1, 0.2, 0.3, 0.4),
y = c(0.1, 0.1, 0.2, 0.3),
z = c(0.1, 0.2, 0.2, 0.2))
df
# A tibble: 4 x 3
x y z
<dbl> <dbl> <dbl>
1 0.1 0.1 0.1
2 0.2 0.1 0.2
3 0.3 0.2 0.2
4 0.4 0.3 0.2
I want to sum across rows and I want to only add up the "cells" that meet a certain logical condition. In this example, I want to add up, rowwise, only cells that contain a equal to or greater than a specified threshold.
Desired Output
threshold <- 0.15
# A tibble: 4 x 4
x y z cond_sum
<dbl> <dbl> <dbl> <dbl>
1 0.1 0.1 0.1 0
2 0.2 0.1 0.2 0.4
3 0.3 0.2 0.2 0.7
4 0.4 0.3 0.2 0.9
Pseudo-code
This is the wrangling idea I have in mind.
df %>%
rowwise() %>%
mutate(cond_sum = sum(c_across(where(~ "cell" >= threshold))))
Tidy solutions appreciated!