# NOT RUN {
#library(rindex)
x <- sample(c(rep("c", 5), paste("d", 1:5, sep=""), c(letters[c(1,2,5,6)], NA)))
cat("\n")
cat("creating an index from atomic vector\n")
i <- index(x)
i
cat("creating an index by combining indices\n")
i2 <- c(i,i)
i2
cat("if the index (or index$tree) is removed, the C-tree is removed at the next garbage collection\n")
cat("the index tree can also removed and created explicitely\n")
i <- indexDelTree(i)
i
i <- indexAddTree(i, batch=3)
print(i, tree=TRUE)
indexNodes(i)
indexBytes(i)
cat("\n")
cat("extracting the original vector\n")
i[]
cat("subsetting works as expected\n")
i[1:3]
cat("accessing the sorted data is much faster\n")
sort(i)[1:3]
cat("accessing the ordering is also much faster (order.index is not dispatched since order is not yet generic)\n")
order.index(i)[1:3]
identical(is.na(i),is.na(x))
identical(length(i),length(x))
cat("\n")
cat("LOW LEVEL SEARCH returns position in SORTED VECTOR\n")
cat("low level search for position of lowest instance of value in the index\n")
indexFind(i, "c")
cat("low level search for position of highest instance of value in the index\n")
indexFind(i, "c", findlow=FALSE)
cat("low level search for position of lowest instance beginning like search value\n")
indexFindlike(i,"d")
cat("low level search for position of highest instance beginning like search value\n")
indexFindlike(i,"d",findlow=FALSE)
cat("\n")
cat("MID LEVEL SEARCH also returns position in SORTED VECTOR\n")
cat("mid level search for a set of values\n")
indexMatch(i,c("c","f"), findlow=TRUE) # giving parameter findlow= suppresses the warning issued on non-unique indices
sort(i)[indexMatch(i,c("c","f"), findlow=TRUE)]
i[indexMatch(i,c("c","f"), findlow=TRUE, what="pos")]
indexMatch(i,c("c","f"), findlow=TRUE, what="val")
indexMatch(i,c("c","f"), findlow=TRUE, what="pos")
indexMatch(i,c("c","f"), findlow=FALSE, what="pos")
cat("mid level search for interval of values\n")
indexFindInterval(i,"b","f")
cat("by default the searched endpoints are included\n")
sort(i)[indexFindInterval(i,"b","f")]
cat("but they can be excluded\n")
sort(i)[indexFindInterval(i,"b","f",high.include=FALSE)]
cat("by default the searched endpoints need not to be present\n")
sort(i)[indexFindInterval(i,"a1","e1")]
cat("but this can be required\n")
sort(i)[indexFindInterval(i,"a1","e1",low.exact=TRUE)]
cat("each of the searched endpoints can be defined via indexFindlike\n")
sort(i)[indexFindInterval(i,"c","d",FUN=indexFindlike)]
cat("\n")
cat("HIGH LEVEL SEARCH returns POSITION(s) IN ORIGINAL VECTOR but in SEQUENCE OF INDEX\n")
indexEQ(i,"d3")
indexNE(i,"d3")
indexLT(i,"d3")
indexLE(i,"d3")
indexGT(i,"d3")
indexGE(i,"d3")
cat("searching for several values returns a list\n")
indexEQ(i,c("b","c","z",NA))
indexEQ(i,c("b","c","z",NA), what="val")
cat("\n")
cat("HIGH LEVEL OPERATORS returns TRUE/FALSE AT ORIGINAL VECTOR POSITIONS\n")
i=="d3"
i!="d3"
i<"d3"
i<="d3"
i>"d3"
i>="d3"
cat("HIGH LEVEL match.index and behave as expected\n")
match(c("b","c","z",NA), x)
match.index(c("b","c","z",NA), i)
# }
# NOT RUN {
# }
# NOT RUN {
cat("function timefactor helps with timing\n")
n <- 1000000
x <- sample(1:n)
names(x) <- paste("a", x, sep="")
d <- data.frame(x=as.vector(x), row.names=names(x))
nsub <- 100
i <- sample(1:n, nsub)
ni <- names(x)[i]
ind <- index(names(x), verbose=TRUE)
ind
# test vectors
cat("character susetting is by magnitude slower than integer subsettting\n")
timefactor( x[ni] , x[i] , 10, 10000)
cat("character susetting is approx as slow as matching\n")
timefactor( x[ni] , x[match(ni,names(x))] , 10, 10)
cat("for small fractions of n indexing is much faster\n")
timefactor( x[ni] , x[indexMatch(ind,ni)] , 10, 100)
# test dataframes
cat("character susetting is by magnitude slower than integer subsettting\n")
timefactor( d[ni,] , d[i,] , 1, 100)
cat("obvious implementation problem (in R-2.5.1 subsetting is much slower than via matching)\n")
timefactor( d[ni,] , d[match(ni,rownames(d)),] , 1, 1)
cat("for small fractions of n indexing is much faster\n")
timefactor( d[ni,] , d[indexMatch(ind,ni),] , 1, 10)
# }
# NOT RUN {
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab