Hi,
It was as I thought then. Here is my implementation:
library(stringr)
library(dplyr)
options(stringsAsFactors = F)
groups <- c("A", "B", "C", "D", "E")
value <- c("9.9-13.9", "32.1-32.9", "0.9-6.9", 73, "14.8 AND 41.1")
data <- cbind(groups, value)
data = data.frame(data)
data = purrr::map_df(1:nrow(data), function(x){ # x = 1
value = data$value[x]
if(str_detect(value, "-")){
myRange = as.numeric(unlist(str_split(data$value[x], "-")))
data.frame(groups = data$groups[x],
start = myRange[1], end = myRange[2], info = "range")
} else if(str_detect(value, "AND")){
myVals = as.numeric(unlist(str_split(data$value[x], "AND")))
data.frame(groups = data$groups[x],
start = myVals, end = myVals, info = "multiple")
} else if(!is.na(as.numeric(value))){
myVals = as.numeric(value)
data.frame(groups = data$groups[x],
start = myVals, end = myVals, info = "single")
} else {
data.frame(groups = data$groups[x],
start = NA, end = NA, info = "error")
}
})
# New input
value2 <- c(73.0, 32.9, 10.0, 6.1, 14.8, 41.1)
sample <- 1:6
data2 <- cbind(sample, value2)
#Find groups for input
data2 = data.frame(data2)
data2 = data2 %>% mutate(groups = sapply(value2, function(x) {
data %>% filter(start <= x, end >= x) %>% pull(groups)
}))
data2
sample value2 groups
1 1 73.0 D
2 2 32.9 B
3 3 10.0 A
4 4 6.1 C
5 5 14.8 E
6 6 41.1 E
Note that this simple version assumes that there is only one group that can match per value, i.e. the ranges of data in the first dataset do not overlap. If they would, the are multiple groups possible per input and the code needs to be expanded (not that difficult)
Hope this helps,
PJ