R Under development (unstable) (2024-08-21 r87038 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library( "intervals" ) > > ######## NAs and empty intervals > > u <- Intervals_full( as.numeric(NA), type = "Z" ) > u <- c( u, u ) > v <- Intervals_full( c(1,3,1,Inf), type = "Z" ) > x <- Intervals( 1, closed = FALSE, type = "Z" ) # empty > w <- c( x, u, v ) Warning message: Coercion to 'Intervals_full' required. > rownames(w) <- letters[ 1:nrow(w) ] > > x Object of class Intervals 1 interval over Z: (1, 1) > u Object of class Intervals_full 2 intervals over Z: [NA, NA] [NA, NA] > v Object of class Intervals_full 2 intervals over Z: [1, 1] [3, Inf] > w Object of class Intervals_full 5 intervals over Z: a (1, 1) b [NA, NA] c [NA, NA] d [1, 1] e [3, Inf] > > is.na(w) a b c d e FALSE TRUE TRUE FALSE FALSE > empty(w) [1] TRUE NA NA FALSE FALSE > > distance_to_nearest( u, v ) [1] NA NA > distance_to_nearest( w, v ) a b c d e NA NA NA 0 0 Warning message: Some empty 'from' intervals encountered. Setting to NA... > > pts <- c( -Inf, Inf, NA, NaN, 0:2 ) > > distance_to_nearest( pts, v ) [1] NA 0 NA NA 1 0 1 > > identical( + distance_to_nearest( pts, v ), + distance_to_nearest( pts, open_intervals( v ) ) + ) [1] TRUE > > interval_overlap( w, v ) $a integer(0) $b integer(0) $c integer(0) $d [1] 1 $e [1] 2 Warning message: Some empty 'from' intervals encountered. Setting to NA... > > reduce( w ) Object of class Intervals_full 2 intervals over Z: (0, 2) (2, Inf) Warning message: Intervals with NA endpoints removed. > > open_intervals(w) Object of class Intervals_full 4 intervals over Z: b (NA, NA) c (NA, NA) d (0, 2) e (2, Inf) Warning message: Empty intervals encountered and removed. > > > > > ######## Subset assignment > > u <- Intervals( 1:8, type = "Z" ) > rownames( u ) <- letters[ 1:nrow(u) ] > v <- Intervals( -4:-1, type = "Z" ) > rownames( v ) <- letters[ nrow(u) + 1:nrow(v) ] > w <- open_intervals(u) > > u Object of class Intervals 4 intervals over Z: a [1, 5] b [2, 6] c [3, 7] d [4, 8] > v Object of class Intervals 2 intervals over Z: e [-4, -2] f [-3, -1] > w Object of class Intervals 4 intervals over Z: a (0, 6) b (1, 7) c (2, 8) d (3, 9) > > # Basic > z <- u; z[2:3,] <- v > z Object of class Intervals 4 intervals over Z: a [1, 5] e [-4, -2] f [-3, -1] d [4, 8] > > # With names > z <- u; z[c("a","d"),] <- v > z Object of class Intervals 4 intervals over Z: e [-4, -2] b [2, 6] c [3, 7] f [-3, -1] > > # Missing row name > result <- try( { z <- u; z[c("a","e"),] <- v }, TRUE ) > result [1] "Error in .local(x, i, j = j, ..., value) : \n Cannot assign to NA indices or row names which do not exist.\n" attr(,"class") [1] "try-error" attr(,"condition") > > # Closure adjustment > z <- w; z[2:3,] <- v > z Object of class Intervals 4 intervals over Z: a (0, 6) e (-5, -1) f (-4, 0) d (3, 9) > > # Size mismatch error > result <- try( { z <- w; z[2:3,] <- v[1,] }, TRUE ) > result [1] "Error in .local(x, i, j = j, ..., value) : \n Replacement object is of the wrong size.\n" attr(,"class") [1] "try-error" attr(,"condition") > > # Intervals_full method > x <- Intervals_full( 1:6, c(TRUE,FALSE) ) > rownames( x ) <- letters[ 1:nrow(x) ] > y <- Intervals_full( -4:-1 ) > > x Object of class Intervals_full 3 intervals over R: a [1, 4) b [2, 5) c [3, 6) > y Object of class Intervals_full 2 intervals over R: [-4, -2] [-3, -1] > > # Missing value names > z <- x; z[2,] <- y[1,] > z Object of class Intervals_full 3 intervals over R: a [1, 4) [-4, -2] c [3, 6) > > # Missing x names > z <- y; z[1,] <- x[1,] > z Object of class Intervals_full 2 intervals over R: a [1, 4) [-3, -1] > > # Type mismatch error > result <- try( { z <- x; z[2:3,] <- v }, TRUE ) > result [1] "Error in .local(x, i, j = j, ..., value) : Types do not match (Z vs. R).\n" attr(,"class") [1] "try-error" attr(,"condition") > > # Coercion up > type(v) <- "R" > closed(v) <- c( FALSE, TRUE ) > x Object of class Intervals_full 3 intervals over R: a [1, 4) b [2, 5) c [3, 6) > v Object of class Intervals 2 intervals over R: e (-4, -2] f (-3, -1] > z <- x; z[2:3,] <- v > z Object of class Intervals_full 3 intervals over R: a [1, 4) e (-4, -2] f (-3, -1] > > # With warning due to assignment > z <- v; z[1,] <- x[3,] Warning message: Coercion to 'Intervals_full' required. > z Object of class Intervals_full 2 intervals over R: c [3, 6) f (-3, -1] > > # No row name copying with matrices > A <- matrix( 0, 2, 2 ) > rownames(A) <- c("1","2") > z <- x; z[1:2,] <- A > z Object of class Intervals_full 3 intervals over R: a [0, 0) b [0, 0) c [3, 6) > > > > > ######## distance_to_nearest() behavior > > a <- Intervals_full( c(2,5), FALSE ) > b <- Intervals_full( c(1,5,2,10), matrix(c(TRUE,FALSE,FALSE,TRUE),2,2) ) > > a Object of class Intervals_full 1 interval over R: (2, 5) > b Object of class Intervals_full 2 intervals over R: [1, 2) (5, 10] > > distance_to_nearest(a,b) [1] 0 > > type(a) <- "Z" > type(b) <- "Z" > > distance_to_nearest(a,b) [1] 2 > > a <- as( a, "Intervals" ) > b <- as( open_intervals( b ), "Intervals" ) > > a Object of class Intervals 1 interval over Z: (2, 5) > b Object of class Intervals 2 intervals over Z: (0, 2) (5, 11) > > distance_to_nearest(a,b) [1] 2 > > type(a) <- "R" > type(b) <- "R" > > distance_to_nearest(a,b) [1] 0 > > proc.time() user system elapsed 0.64 0.06 0.70