1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| d <- data.frame(debut = c(1, 3, 7, 11), fin = c(5, 10, 9, 13), valeur = c("w", "x", "y", "z"))
p <- unique(c(d$debut, d$fin)
#dev_ggy
alternative_1 <- function() {
df<-sort(p)
seg <- data.frame(deb=numeric(0),fin=numeric(0))
for (i in 1:(length(df)-1)){
seg[i, 1]<-df[i]
seg[i, 2]<-df[i+1]
}
seg[,3] <- ""
for (i in 1:(length(df)-1)){
for (j in 1:length(d[,1])){
if (d[j,1]<=seg[i,1] && seg[i,2]<=d[j,2]){
if(is.na(seg[i,3])){
seg[i,3]<-d[j,3]
} else{
seg[i,3]<-paste(seg[i,3],d[j,3])
}
}
}
}
seg
}
#Sengar
alternative_2 <- function() {
n<-p[order(p)]
d2<-expand.grid(valeur=d$valeur,debut=n[1:(length(n)-1)])
d2$fin<-n[match(d2$debut,n)+1]
d2$is.in<-d$debut[match(d2$valeur,d$valeur)]<=d2$debut & d$fin[match(d2$valeur,d$valeur)]>=d2$fin
d2<-d2[d2$is.in,]
aggregate(data=d2,valeur~debut+fin,paste)
}
alternative_3 <- function(x) {
fct <- function(x) {
extremites.courantes <- combn(sort(c(x, p[which(p < x)])), 2)
localiser.intersections <- function(x) Position(function(y) y == x, extremites.courantes[1,])
intersections <- sapply(unique(extremites.courantes[1,]), localiser.intersections)
extremites.courantes[,intersections]
}
matrix(sapply(x, fct), nrow = 2)
} |
Partager