I am trying to create vectorized conditional outputs of a data frame.
Suppose I have the dataframe:
data <- data.frame(a = c(5, 3, 9, 5),
b = c(1, 2, 3, 4),
c = c(5, 3, 9, 5),
d = c(5, 3, 9, 5))
And the threshold:
threshold <- c(a1 = 4, b1 = 2, c1 = 8, d1 = 2)
What I want is a new dataset that indicates whether each value of a is greater than or equal to the corresponding value in the threshold vector (a1), each value of b is greater than or equal to the corresponding value in the threshold vector (b1), etc.
So the desired output would be:
desired_data <- data.frame(a = c(1, 0, 1, 1),
b = c(0, 1, 1, 1),
c = c(0, 0, 1, 0),
d = c(1, 1, 1, 1))
I want to do this as simply as possible, ideally using a purrr function.
Here is a wrong attempt:
desired_data <- map(data >= threshold)
I feel like map2 might be promising, and have checked the documentation (e.g., here and here) but I can't seem to get the syntax for conditional outputs based on mapping.
Thank you!