Reputation: 69
In R ;
I want to put that 'points' according to that intervals in my table
df <- data.frame(
category=c("aa","aa","aa","bb","bb","bb"),
intervals = c("[-Inf,20)", "[20,212)", "[212,Inf)", "[-Inf,150)", "[150,260)","[260,Inf)"),
points = c(45, 15, 35, -15,10,35))
the case is :
if 'category'=aa then if 'intervals'=[20,212) then points=15
but it should not be manual like that it should work related to my below table columns!
Thanks!
Upvotes: 1
Views: 425
Reputation: 39647
You can use findInterval
to link points
to a number.
y$points <- sapply(seq_len(nrow(y)), function(i) {
x[[y$category[i]]]$points[findInterval(y$intervals[i], x[[y$category[i]]]$intervalls)]
})
y
# category intervals points
#1 aa -50 45
#2 aa 25 15
#3 aa 55 15
#4 aa 250 35
#5 bb 5 -15
#6 bb 170 10
#7 bb 290 35
Data:
x <- list(aa=list(intervalls=c(-Inf, 20, 212, Inf), points=c(45,15,35))
,bb=list(intervalls=c(-Inf, 150, 260, Inf), points=c(-15,10,35)))
y <- data.frame(category=c("aa","aa","aa","aa","bb","bb","bb")
, intervals=c(-50,25,55,250,5,170,290))
Create x
from df
given in the question:
tt <- lapply(strsplit(gsub("[[)]", "", df$intervals), ","), as.numeric)
x <- lapply(unique(df$category), function(i) {
list(intervalls=sort(unique(unlist(tt[which(df$category == i)])))
, points=df$points[df$category==i][order(unlist(lapply(tt[which(df$category == i)], "[[", 2)))])
})
names(x) <- unique(df$category)
Upvotes: 1