R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 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 a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("gtools-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('gtools') > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "combinations" > > ### * combinations > > flush(stderr()); flush(stdout()) > > ### Name: combinations > ### Title: Enumerate the Combinations or Permutations of the Elements of a > ### Vector > ### Aliases: combinations permutations > ### Keywords: manip > > ### ** Examples > > combinations(3,2,letters[1:3]) [,1] [,2] [1,] "a" "b" [2,] "a" "c" [3,] "b" "c" > combinations(3,2,letters[1:3],repeats=TRUE) [,1] [,2] [1,] "a" "a" [2,] "a" "b" [3,] "a" "c" [4,] "b" "b" [5,] "b" "c" [6,] "c" "c" > > permutations(3,2,letters[1:3]) [,1] [,2] [1,] "a" "b" [2,] "a" "c" [3,] "b" "a" [4,] "b" "c" [5,] "c" "a" [6,] "c" "b" > permutations(3,2,letters[1:3],repeats=TRUE) [,1] [,2] [1,] "a" "a" [2,] "a" "b" [3,] "a" "c" [4,] "b" "a" [5,] "b" "b" [6,] "b" "c" [7,] "c" "a" [8,] "c" "b" [9,] "c" "c" > > # To use large 'n', you need to change the default recusion limit > options(expressions=1e5) > cmat <- combinations(300,2) > dim(cmat) # 44850 by 2 [1] 44850 2 > > > > cleanEx(); ..nameEx <- "dirichlet" > > ### * dirichlet > > flush(stderr()); flush(stdout()) > > ### Name: rdirichlet > ### Title: Functions for the Dirichlet Distribution > ### Aliases: rdirichlet ddirichlet > ### Keywords: distribution > > ### ** Examples > > > x <- rdirichlet(20, c(1,1,1) ) > > ddirichlet(x, c(1,1,1) ) [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 > > > > > cleanEx(); ..nameEx <- "foldchange" > > ### * foldchange > > flush(stderr()); flush(stdout()) > > ### Name: foldchange > ### Title: Compute fold-change or convert between log-ratio and > ### fold-change. > ### Aliases: foldchange logratio2foldchange foldchange2logratio > ### Keywords: math > > ### ** Examples > > > a <- 1:21 > b <- 21:1 > > f <- foldchange(a,b) > > cbind(a,b,f) a b f [1,] 1 21 -21.000000 [2,] 2 20 -10.000000 [3,] 3 19 -6.333333 [4,] 4 18 -4.500000 [5,] 5 17 -3.400000 [6,] 6 16 -2.666667 [7,] 7 15 -2.142857 [8,] 8 14 -1.750000 [9,] 9 13 -1.444444 [10,] 10 12 -1.200000 [11,] 11 11 1.000000 [12,] 12 10 1.200000 [13,] 13 9 1.444444 [14,] 14 8 1.750000 [15,] 15 7 2.142857 [16,] 16 6 2.666667 [17,] 17 5 3.400000 [18,] 18 4 4.500000 [19,] 19 3 6.333333 [20,] 20 2 10.000000 [21,] 21 1 21.000000 > > > > > cleanEx(); ..nameEx <- "invalid" > > ### * invalid > > flush(stderr()); flush(stdout()) > > ### Name: invalid > ### Title: Test if a value is missing, empty, or contains only NA or NULL > ### values > ### Aliases: invalid > ### Keywords: programming > > ### ** Examples > > > invalid(NA) [1] TRUE > invalid() [1] TRUE > invalid(c(NA,NA,NULL,NA)) [1] TRUE > > invalid(list(a=1,b=NULL)) [1] FALSE > > # example use in a function > myplot <- function(x,y) { + if(invalid(y)) { + y <- x + x <- 1:length(y) + } + plot(x,y) + } > myplot(1:10) > myplot(1:10,NA) > > > > cleanEx(); ..nameEx <- "logit" > > ### * logit > > flush(stderr()); flush(stdout()) > > ### Name: logit > ### Title: Generalized logit and inverse logit function > ### Aliases: logit inv.logit > ### Keywords: math > > ### ** Examples > > > x <- seq(0,10, by=0.25) > xt <- logit(x, min=0, max=10) > cbind(x,xt) x xt [1,] 0.00 -Inf [2,] 0.25 -3.6635616 [3,] 0.50 -2.9444390 [4,] 0.75 -2.5123056 [5,] 1.00 -2.1972246 [6,] 1.25 -1.9459101 [7,] 1.50 -1.7346011 [8,] 1.75 -1.5505974 [9,] 2.00 -1.3862944 [10,] 2.25 -1.2367626 [11,] 2.50 -1.0986123 [12,] 2.75 -0.9694006 [13,] 3.00 -0.8472979 [14,] 3.25 -0.7308875 [15,] 3.50 -0.6190392 [16,] 3.75 -0.5108256 [17,] 4.00 -0.4054651 [18,] 4.25 -0.3022809 [19,] 4.50 -0.2006707 [20,] 4.75 -0.1000835 [21,] 5.00 0.0000000 [22,] 5.25 0.1000835 [23,] 5.50 0.2006707 [24,] 5.75 0.3022809 [25,] 6.00 0.4054651 [26,] 6.25 0.5108256 [27,] 6.50 0.6190392 [28,] 6.75 0.7308875 [29,] 7.00 0.8472979 [30,] 7.25 0.9694006 [31,] 7.50 1.0986123 [32,] 7.75 1.2367626 [33,] 8.00 1.3862944 [34,] 8.25 1.5505974 [35,] 8.50 1.7346011 [36,] 8.75 1.9459101 [37,] 9.00 2.1972246 [38,] 9.25 2.5123056 [39,] 9.50 2.9444390 [40,] 9.75 3.6635616 [41,] 10.00 Inf > > y <- inv.logit(xt, min=0, max=10) > cbind(x,xt,y) x xt y [1,] 0.00 -Inf 0.00 [2,] 0.25 -3.6635616 0.25 [3,] 0.50 -2.9444390 0.50 [4,] 0.75 -2.5123056 0.75 [5,] 1.00 -2.1972246 1.00 [6,] 1.25 -1.9459101 1.25 [7,] 1.50 -1.7346011 1.50 [8,] 1.75 -1.5505974 1.75 [9,] 2.00 -1.3862944 2.00 [10,] 2.25 -1.2367626 2.25 [11,] 2.50 -1.0986123 2.50 [12,] 2.75 -0.9694006 2.75 [13,] 3.00 -0.8472979 3.00 [14,] 3.25 -0.7308875 3.25 [15,] 3.50 -0.6190392 3.50 [16,] 3.75 -0.5108256 3.75 [17,] 4.00 -0.4054651 4.00 [18,] 4.25 -0.3022809 4.25 [19,] 4.50 -0.2006707 4.50 [20,] 4.75 -0.1000835 4.75 [21,] 5.00 0.0000000 5.00 [22,] 5.25 0.1000835 5.25 [23,] 5.50 0.2006707 5.50 [24,] 5.75 0.3022809 5.75 [25,] 6.00 0.4054651 6.00 [26,] 6.25 0.5108256 6.25 [27,] 6.50 0.6190392 6.50 [28,] 6.75 0.7308875 6.75 [29,] 7.00 0.8472979 7.00 [30,] 7.25 0.9694006 7.25 [31,] 7.50 1.0986123 7.50 [32,] 7.75 1.2367626 7.75 [33,] 8.00 1.3862944 8.00 [34,] 8.25 1.5505974 8.25 [35,] 8.50 1.7346011 8.50 [36,] 8.75 1.9459101 8.75 [37,] 9.00 2.1972246 9.00 [38,] 9.25 2.5123056 9.25 [39,] 9.50 2.9444390 9.50 [40,] 9.75 3.6635616 9.75 [41,] 10.00 Inf 10.00 > > > > > cleanEx(); ..nameEx <- "mixedsort" > > ### * mixedsort > > flush(stderr()); flush(stdout()) > > ### Name: mixedsort > ### Title: Order or Sort strings with embedded numbers so that the numbers > ### are in the correct order > ### Aliases: mixedsort mixedorder > ### Keywords: univar manip > > ### ** Examples > > # compound & dose labels > Treatment <- c("Control", "Asprin 10mg/day", "Asprin 50mg/day", + "Asprin 100mg/day", "Acetomycin 100mg/day", + "Acetomycin 1000mg/day") > > # ordinary sort puts the dosages in the wrong order > sort(Treatment) [1] "Acetomycin 1000mg/day" "Acetomycin 100mg/day" "Asprin 100mg/day" [4] "Asprin 10mg/day" "Asprin 50mg/day" "Control" > > # but mixedsort does the 'right' thing > mixedsort(Treatment) Warning: no finite arguments to max; returning -Inf [1] "Acetomycin 100mg/day" "Acetomycin 1000mg/day" "Asprin 10mg/day" [4] "Asprin 50mg/day" "Asprin 100mg/day" "Control" > > # Here is a more complex example > x <- rev(c("AA 0.50 ml", "AA 1.5 ml", "AA 500 ml", "AA 1500 ml", + "EXP 1", "AA 1e3 ml", "A A A", "1 2 3 A", "NA", NA, "1e2", + "", "-", "1A", "1 A", "100", "100A", "Inf")) > > mixedorder(x) [1] 7 11 4 5 3 8 2 1 6 12 18 17 16 13 15 14 10 9 > > mixedsort(x) [1] "" "1 2 3 A" "1 A" "1A" "100" [6] "1e2" "100A" "Inf" "-" "A A A" [11] "AA 0.50 ml" "AA 1.5 ml" "AA 500 ml" "AA 1e3 ml" "AA 1500 ml" [16] "EXP 1" "NA" NA > # notice that plain numbers, including 'Inf' show up before strings. > > > > > cleanEx(); ..nameEx <- "oddeven" > > ### * oddeven > > flush(stderr()); flush(stdout()) > > ### Name: odd > ### Title: Detect odd/even integers > ### Aliases: odd even > ### Keywords: arith > > ### ** Examples > > > odd(4) [1] FALSE > even(4) [1] TRUE > > odd(1:10) [1] TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE > even(1:10) [1] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE > > > > > cleanEx(); ..nameEx <- "permute" > > ### * permute > > flush(stderr()); flush(stdout()) > > ### Name: permute > ### Title: Randomly Permute the Elements of a Vector > ### Aliases: permute > ### Keywords: distribution > > ### ** Examples > > x <- 1:10 > permute(x) [1] 3 4 5 7 2 8 9 6 10 1 > > > > cleanEx(); ..nameEx <- "quantcut" > > ### * quantcut > > flush(stderr()); flush(stdout()) > > ### Name: quantcut > ### Title: Create a Factor Variable Using the Quantiles of a Continuous > ### Variable > ### Aliases: quantcut > ### Keywords: manip > > ### ** Examples > > > ## create example data > ## Don't show: > set.seed(1234) > > ## End Don't show > x <- rnorm(1000) > > ## cut into quartiles > quartiles <- quantcut( x ) > table(quartiles) quartiles [-3.4,-0.673] (-0.673,-0.0398] (-0.0398,0.616] (0.616,3.2] 250 250 250 250 > > ## cut into deciles > deciles <- quantcut( x, seq(0,1,by=0.1) ) > table(deciles) deciles [-3.4,-1.21] (-1.21,-0.849] (-0.849,-0.538] (-0.538,-0.285] 100 100 100 100 (-0.285,-0.0398] (-0.0398,0.193] (0.193,0.466] (0.466,0.761] 100 100 100 100 (0.761,1.33] (1.33,3.2] 100 100 > > ## show handling of 'tied' quantiles. > x <- round(x) # discretize to create ties > stem(x) # display the ties The decimal point is at the | -3 | 0000000000000 -2 | -2 | 000000000000000000000000000000000000000000000000 -1 | -1 | 00000000000000000000000000000000000000000000000000000000000000000000+181 -0 | -0 | 0 | 00000000000000000000000000000000000000000000000000000000000000000000+310 0 | 1 | 00000000000000000000000000000000000000000000000000000000000000000000+140 1 | 2 | 000000000000000000000000000000000000000000000000000000000000000 2 | 3 | 00000 > deciles <- quantcut( x, seq(0,1,by=0.1) ) > > table(deciles) # note that there are only 5 groups (not 10) deciles [-3,-1) -1 0 1 (1,3] 61 261 390 220 68 > # due to duplicates > > > > > cleanEx(); ..nameEx <- "running" > > ### * running > > flush(stderr()); flush(stdout()) > > ### Name: running > ### Title: Apply a Function Over Adjacent Subsets of a Vector > ### Aliases: running > ### Keywords: misc > > ### ** Examples > > > # show effect of pad > running(1:20, width=5) 1:5 2:6 3:7 4:8 5:9 6:10 7:11 8:12 9:13 10:14 11:15 12:16 13:17 3 4 5 6 7 8 9 10 11 12 13 14 15 14:18 15:19 16:20 16 17 18 > running(1:20, width=5, pad=TRUE) 1:1 1:2 1:3 1:4 1:5 2:6 3:7 4:8 5:9 6:10 7:11 8:12 9:13 NA NA NA NA 3 4 5 6 7 8 9 10 11 10:14 11:15 12:16 13:17 14:18 15:19 16:20 12 13 14 15 16 17 18 > > # show effect of align > running(1:20, width=5, align="left", pad=TRUE) 1:5 2:6 3:7 4:8 5:9 6:10 7:11 8:12 9:13 10:14 11:15 12:16 13:17 3 4 5 6 7 8 9 10 11 12 13 14 15 14:18 15:19 16:20 17:20 18:20 19:20 20:20 16 17 18 NA NA NA NA > running(1:20, width=5, align="center", pad=TRUE) 1:1 1:2 1:3 1:4 1:5 2:6 3:7 4:8 5:9 6:10 7:11 8:12 9:13 NA NA NA NA 3 4 5 6 7 8 9 10 11 10:14 11:15 12:16 13:17 14:18 15:19 16:20 17:20 18:20 19:20 20:20 12 13 14 15 16 17 18 NA NA NA NA > running(1:20, width=5, align="right", pad=TRUE) 1:1 1:2 1:3 1:4 1:5 2:6 3:7 4:8 5:9 6:10 7:11 8:12 9:13 NA NA NA NA 3 4 5 6 7 8 9 10 11 10:14 11:15 12:16 13:17 14:18 15:19 16:20 12 13 14 15 16 17 18 > > # show effect of simplify > running(1:20, width=5, fun=function(x) x ) # matrix 1:5 2:6 3:7 4:8 5:9 6:10 7:11 8:12 9:13 10:14 11:15 12:16 13:17 14:18 [1,] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 [2,] 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [3,] 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [4,] 4 5 6 7 8 9 10 11 12 13 14 15 16 17 [5,] 5 6 7 8 9 10 11 12 13 14 15 16 17 18 15:19 16:20 [1,] 15 16 [2,] 16 17 [3,] 17 18 [4,] 18 19 [5,] 19 20 > running(1:20, width=5, fun=function(x) x, simplify=FALSE) # list $"1:5" [1] 1 2 3 4 5 $"2:6" [1] 2 3 4 5 6 $"3:7" [1] 3 4 5 6 7 $"4:8" [1] 4 5 6 7 8 $"5:9" [1] 5 6 7 8 9 $"6:10" [1] 6 7 8 9 10 $"7:11" [1] 7 8 9 10 11 $"8:12" [1] 8 9 10 11 12 $"9:13" [1] 9 10 11 12 13 $"10:14" [1] 10 11 12 13 14 $"11:15" [1] 11 12 13 14 15 $"12:16" [1] 12 13 14 15 16 $"13:17" [1] 13 14 15 16 17 $"14:18" [1] 14 15 16 17 18 $"15:19" [1] 15 16 17 18 19 $"16:20" [1] 16 17 18 19 20 > > # show effect of by > running(1:20, width=5) # normal 1:5 2:6 3:7 4:8 5:9 6:10 7:11 8:12 9:13 10:14 11:15 12:16 13:17 3 4 5 6 7 8 9 10 11 12 13 14 15 14:18 15:19 16:20 16 17 18 > running(1:20, width=5, by=5) # non-overlapping 1:5 6:10 11:15 16:20 3 8 13 18 > running(1:20, width=5, by=2) # starting every 2nd 1:5 3:7 5:9 7:11 9:13 11:15 13:17 15:19 3 5 7 9 11 13 15 17 > > # Use 'pad' to ensure correct length of vector, also show the effect > # of allow.fewer. > par(mfrow=c(2,1)) > plot(1:20, running(1:20, width=5, allow.fewer=FALSE, pad=TRUE), type="b") > plot(1:20, running(1:20, width=5, allow.fewer=TRUE, pad=TRUE), type="b") > par(mfrow=c(1,1)) > > # plot running mean and central 2 standard deviation range > # estimated by *last* 40 observations > dat <- rnorm(500, sd=1 + (1:500)/500 ) > plot(dat) > sdfun <- function(x,sign=1) mean(x) + sign * sqrt(var(x)) > lines(running(dat, width=51, pad=TRUE, fun=mean), col="blue") > lines(running(dat, width=51, pad=TRUE, fun=sdfun, sign=-1), col="red") > lines(running(dat, width=51, pad=TRUE, fun=sdfun, sign= 1), col="red") > > # plot running correlation estimated by last 40 observations (red) > # against the true local correlation (blue) > sd.Y <- seq(0,1,length=500) > > X <- rnorm(500, sd=1) > Y <- rnorm(500, sd=sd.Y) > > plot(running(X,X+Y,width=20,fun=cor,pad=TRUE),col="red",type="s") > > r <- 1 / sqrt(1 + sd.Y^2) # true cor of (X,X+Y) > lines(r,type="l",col="blue") > > > > graphics::par(get("par.postscript", env = .CheckExEnv)) > cleanEx(); ..nameEx <- "scat" > > ### * scat > > flush(stderr()); flush(stdout()) > > ### Name: scat > ### Title: Display debugging text > ### Aliases: scat > ### Keywords: print > > ### ** Examples > > options(DEBUG=NULL) # makee sure DEBUG isn't set > scat("Not displayed") > > options(DEBUG=TRUE) > scat("This will be displayed immediately (even in R BATCH output \n") ### This will be displayed immediately (even in R BATCH output ### > scat("files), provided options()$DEBUG is TRUE.") ### files), provided options()$DEBUG is TRUE. ### > > > > ### *