I have three columns, one is the minimum price for a product, another is a maximum price for the product, and the actual price for a product. I'm trying to go row by row and compare the minimum and maximum price with the actual price to see if it is within that range.
Here is some sample data:
MinimumPrice MaximumPrice ActualPrice
30.5 41 51
95.5 100 92
45.5 50 43
70 75 80
I tried to use:
TBAcomparison$withinRange_Price<- ifelse(sapply(Prices$ActualPrice, function(p)
any(Prices$MaximumPrice <= p & Prices$MinimumPrice>= p)),1, NA)
Although, this isn't exactly what I'm looking for.
The result should be:
MinimumPrice MaximumPrice ActualPrice WithinRange
30.5 41 51 1
95.5 100 92 NA
42.5 50 43 1
70 75 80 NA
Anyone able to assist would be much appreciated.
dput:
structure(list(MinimumPrice = c(30.5, 95.5, 45.5, 70), MaximumPrice = c(41,
100, 50, 75), ActualPrice = c(51, 92, 43, 80), withinrange = c("No",
"No", "No", "No")), row.names = c(NA, -4L), class = c("tbl_df",
"tbl", "data.frame"))
An option with dplyr
library(dplyr)
Prices %>%
mutate(withinRange_Price = case_when(ActualPrice > MinimumPrice &
ActualPrice <= MaximumPrice ~ 1))