Reputation: 2505
I've looked around but could not find an answer that helps. I'm trying to set up a key-value table in R where the key is defined as range or interval so that I can do a look-up to find the value of any key that falls within the range / interval. To give a minimal example which does not work.
data.frame(key = c(range(0, 1),
range(1, 2)),
value = c("A",
"B"))
The data frame would not hold two rows but four since R is treating lower and upper interval bound as two separate values and recycle the value column to arrive at four rows. Besides this problem, I could also not find a range / interval function where I can define lower and upper bounds.
EDIT: The desired output would be a table like this:
key value
(0, 1) "A"
(1, 2) "B"
So I could do something like the following query for a value a: table$value[where a %in% table$key]
Upvotes: 1
Views: 5465
Reputation: 981
If I understood it correctly, you want to find the interval where each value occurs. If that is the case, then you can do so by finding the minimum and maximum "key" numbers for each of the "value" numbers.
This can be easily achieved using the dplyr
package as follows:
#If not installed do install.packages("dplyr")
library(dplyr)
#Provided example
df = data.frame(key = c(range(0, 1),range(1, 2)),
value = c("A", "B"))
#First group data by "value" variable
# and then paste a vector with min and max of "key" variable
df2 = df %>%
group_by(value) %>%
summarise(key = paste0("(", min(key), ",", max(key), ")"))
Result:
value key
1 A (0,1)
2 B (1,2)
You can easily get the values in an interval by doing:
subset(df2, key == "(0,1)")$value
This solution is general (it doesn't matter how many values you have). Also, if that is of interest, you could just as easily return minimum and maximum columns, instead of the format (min,max)
df3 = df %>%
group_by(value) %>%
summarise(min = min(key),
max = max(key))
Results in:
value min max
1 A 0 1
2 B 1 2
And you can get the desired interval by doing:
subset(df3, min >= 0 & max <= 1)$value
Upvotes: 1
Reputation: 31161
It is not really elegant but you can do this to make it practical and handy:
df = data.frame(values=letters[1:2])
df$keys=list(0:1, 1:2)
# values keys
#1 a 0, 1
#2 b 1, 2
So that you do not need regex
like when accessing the data with df$keys
.
Upvotes: 0