R Under development (unstable) (2023-11-16 r85542 ucrt) -- "Unsuffered Consequences" Copyright (C) 2023 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("R.utils") Loading required package: R.oo Loading required package: R.methodsS3 R.methodsS3 v1.8.2 (2022-06-13 22:00:14 UTC) successfully loaded. See ?R.methodsS3 for help. R.oo v1.25.0 (2022-06-12 02:20:02 UTC) successfully loaded. See ?R.oo for help. Attaching package: 'R.oo' The following object is masked from 'package:R.methodsS3': throw The following objects are masked from 'package:methods': getClasses, getMethods The following objects are masked from 'package:base': attach, detach, load, save R.utils v2.12.3 successfully loaded. See ?R.utils for help. Attaching package: 'R.utils' The following object is masked from 'package:utils': timestamp The following objects are masked from 'package:base': cat, commandArgs, getOption, isOpen, nullfile, parse, warnings > > > # Insert NAs (default) between all values > y <- c(a=1, b=2, c=3) > print(y) a b c 1 2 3 > x <- insert(y, ats=2:length(y)) > Ex <- c(y[1], NA_real_, y[2], NA_real_, y[3]) > print(x) a b c 1 NA 2 NA 3 > stopifnot(identical(x,Ex)) > > # Insert at first position > y <- c(a=1, b=2, c=3) > print(y) a b c 1 2 3 > x <- insert(y, ats=1, values=rep(NA_real_, 2)) > Ex <- c(NA_real_,NA_real_,y) > print(x) a b c NA NA 1 2 3 > stopifnot(identical(x,Ex)) > > x <- insert(y, ats=1, values=rep(NA_real_,2), useNames=FALSE) > print(x) [1] NA NA 1 2 3 > > # Insert at last position (names of 'values' are ignored > # because input vector has no names) > x <- insert(1:3, ats=4, values=c(d=2, e=1)) > Ex <- c(1:3,2,1) > print(x) [1] 1 2 3 2 1 > stopifnot(identical(x,Ex)) > > > # Insert in the middle of a vector > x <- insert(c(1,3,2,1), ats=2, values=2) > print(x) [1] 1 2 3 2 1 > stopifnot(identical(as.double(x),as.double(Ex))) > > > # Insert multiple vectors at multiple indices at once > x0 <- c(1:4, 8:11, 13:15) > > x <- insert(x0, ats=c(5,9), values=list(5:7,12)) > print(x) [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 > Ex <- 1:max(x) > stopifnot(identical(as.double(x),as.double(Ex))) > > x <- insert(x0, ats=c(5,9,12), values=list(5:7,12,16:18)) > print(x) [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 > Ex <- 1:max(x) > stopifnot(identical(as.double(x),as.double(Ex))) > > > # Insert with duplicated locations (which requires grouping) > x <- letters[1:6] > ats <- c(1L, 1L, 4L) > values <- LETTERS[1:3] > y0 <- c("A", "B", "a", "b", "c", "C", "d", "e", "f") > y <- insert(x, ats = ats, values = values) > print(y) [1] "A" "B" "a" "b" "c" "C" "d" "e" "f" > stopifnot(identical(y, y0)) > > ats <- c(1L, 4L) > values <- list(LETTERS[1:2], LETTERS[3]) > y <- insert(x, ats = ats, values = values) > print(y) [1] "A" "B" "a" "b" "c" "C" "d" "e" "f" > stopifnot(identical(y, y0)) > > > # Insert missing indices > Ex <- 1:20 > missing <- setdiff(Ex, x0) > x <- x0 > for (m in missing) + x <- insert(x, ats=m, values=m) > print(x) [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 > stopifnot(identical(as.double(x),as.double(Ex))) > > > ## Exception handling > x <- 1:10 > res <- try(y <- insert(x, ats=1:2, values=1:3), silent=TRUE) > stopifnot(inherits(res, "try-error")) > > proc.time() user system elapsed 0.23 0.15 0.37