R : Copyright 2004, The R Foundation for Statistical Computing Version 2.0.1 (2004-11-15), 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. [Previously saved workspace restored] > options(STERM='iESS', editor='emacsclient') > source("walk1d.r", echo = verbose) > prob.of.ret() [,1] 2 0.508 4 0.137 6 0.057 8 0.041 10 0.017 12 0.015 14 0.013 16 0.011 18 0.01 20 0.007 22 0.009 24 0.004 26 0.005 28 0.006 30 0.007 32 0.005 34 0.005 36 0.004 38 0.005 40 0.007 42 0.002 44 0.005 46 0.004 48 0.003 50 0.002 52 0.002 54 0.005 56 0.005 58 0.002 66 0.001 68 0.001 70 0.002 72 0.001 74 0.003 76 0.001 78 0.002 80 0.002 82 0.002 86 0.003 88 0.002 92 0.001 96 0.002 100 0.001 > source("walk1d.r", echo = verbose) Error in source("walk1d.r", echo = verbose) : Object "verbose" not found > source("walk1d.r", echo = "verbose") Error in source("walk1d.r", echo = "verbose") : echo must be logical > source("walk1d.r", echo = TRUE) > prob.of.ret <- function(nsteps = 100, nruns = 1000) { rt <- NULL for (i in 1:nruns) { rt <- c(rt, match(0, cumsum(sample(c(-1, 1), nst .... [TRUNCATED] > source("walk1d.r", echo = TRUE) > prob.of.ret <- function(nsteps = 100, nruns = 1000) { rt <- NULL for (i in 1:nruns) { rt <- c(rt, match(0, cumsum(sample(c(-1, 1), nst .... [TRUNCATED] > x <- 1 > x [1] 1 > x <- c(1, 2, 7) > x [1] 1 2 7 > x[1] [1] 1 > x[2] [1] 2 > x[3] [1] 7 > y <- c("a", "bc", "d") > x [1] 1 2 7 > y [1] "a" "bc" "d" > c(x,y) [1] "1" "2" "7" "a" "bc" "d" > lst <- list(age = 40, height=5.9, colors=c("red","blue")) > lst $age [1] 40 $height [1] 5.9 > lst <- list(age = 40, height=5.9, colors=c("red","blue")) > ls function (name, pos = -1, envir = as.environment(pos), all.names = FALSE, pattern) { if (!missing(name)) { nameValue <- try(name) if (identical(class(nameValue), "try-error")) { name <- substitute(name) if (!is.character(name)) name <- deparse(name) pos <- name } else pos <- nameValue } all.names <- .Internal(ls(envir, all.names)) if (!missing(pattern)) { if ((ll <- length(grep("[", pattern, fixed = TRUE))) > 0 && ll != length(grep("]", pattern, fixed = TRUE))) { if (pattern == "[") { pattern <- "\\[" warning(paste("replaced regular expression pattern", sQuote("["), "by", sQuote("\\\\["))) } else if (length(grep("[^\\\\]\\[<-", pattern) > 0)) { pattern <- sub("\\[<-", "\\\\\\[<-", pattern) warning(paste("replaced", sQuote("[<-"), "by", sQuote("\\\\[<-"), "in regular expression pattern")) } } grep(pattern, all.names, value = TRUE) } else all.names } > ls function (name, pos = -1, envir = as.environment(pos), all.names = FALSE, pattern) { if (!missing(name)) { nameValue <- try(name) if (identical(class(nameValue), "try-error")) { name <- substitute(name) if (!is.character(name)) name <- deparse(name) pos <- name } else pos <- nameValue } all.names <- .Internal(ls(envir, all.names)) if (!missing(pattern)) { if ((ll <- length(grep("[", pattern, fixed = TRUE))) > 0 && ll != length(grep("]", pattern, fixed = TRUE))) { if (pattern == "[") { pattern <- "\\[" warning(paste("replaced regular expression pattern", sQuote("["), "by", sQuote("\\\\["))) } else if (length(grep("[^\\\\]\\[<-", pattern) > 0)) { pattern <- sub("\\[<-", "\\\\\\[<-", pattern) warning(paste("replaced", sQuote("[<-"), "by", sQuote("\\\\[<-"), "in regular expression pattern")) } } grep(pattern, all.names, value = TRUE) } else all.names } > lst $age [1] 40 $height [1] 5.9 $colors [1] "red" "blue" > ls function (name, pos = -1, envir = as.environment(pos), all.names = FALSE, pattern) { if (!missing(name)) { nameValue <- try(name) if (identical(class(nameValue), "try-error")) { name <- substitute(name) if (!is.character(name)) name <- deparse(name) pos <- name } else pos <- nameValue } all.names <- .Internal(ls(envir, all.names)) if (!missing(pattern)) { if ((ll <- length(grep("[", pattern, fixed = TRUE))) > 0 && ll != length(grep("]", pattern, fixed = TRUE))) { if (pattern == "[") { pattern <- "\\[" warning(paste("replaced regular expression pattern", sQuote("["), "by", sQuote("\\\\["))) } else if (length(grep("[^\\\\]\\[<-", pattern) > 0)) { pattern <- sub("\\[<-", "\\\\\\[<-", pattern) warning(paste("replaced", sQuote("[<-"), "by", sQuote("\\\\[<-"), "in regular expression pattern")) } } grep(pattern, all.names, value = TRUE) } else all.names } > ls function (name, pos = -1, envir = as.environment(pos), all.names = FALSE, pattern) { if (!missing(name)) { nameValue <- try(name) if (identical(class(nameValue), "try-error")) { name <- substitute(name) if (!is.character(name)) name <- deparse(name) pos <- name } else pos <- nameValue } all.names <- .Internal(ls(envir, all.names)) if (!missing(pattern)) { if ((ll <- length(grep("[", pattern, fixed = TRUE))) > 0 && ll != length(grep("]", pattern, fixed = TRUE))) { if (pattern == "[") { pattern <- "\\[" warning(paste("replaced regular expression pattern", sQuote("["), "by", sQuote("\\\\["))) } else if (length(grep("[^\\\\]\\[<-", pattern) > 0)) { pattern <- sub("\\[<-", "\\\\\\[<-", pattern) warning(paste("replaced", sQuote("[<-"), "by", sQuote("\\\\[<-"), "in regular expression pattern")) } } grep(pattern, all.names, value = TRUE) } else all.names } > ls() [1] "lst" "prob.of.ret" "x" "y" > ls function (name, pos = -1, envir = as.environment(pos), all.names = FALSE, pattern) { if (!missing(name)) { nameValue <- try(name) if (identical(class(nameValue), "try-error")) { name <- substitute(name) if (!is.character(name)) name <- deparse(name) pos <- name } else pos <- nameValue } all.names <- .Internal(ls(envir, all.names)) if (!missing(pattern)) { if ((ll <- length(grep("[", pattern, fixed = TRUE))) > 0 && ll != length(grep("]", pattern, fixed = TRUE))) { if (pattern == "[") { pattern <- "\\[" warning(paste("replaced regular expression pattern", sQuote("["), "by", sQuote("\\\\["))) } else if (length(grep("[^\\\\]\\[<-", pattern) > 0)) { pattern <- sub("\\[<-", "\\\\\\[<-", pattern) warning(paste("replaced", sQuote("[<-"), "by", sQuote("\\\\[<-"), "in regular expression pattern")) } } grep(pattern, all.names, value = TRUE) } else all.names } > prob.of.ret function(nsteps = 100, nruns = 1000) { rt <- NULL; for(i in 1:nruns) { rt <- c(rt, match(0, cumsum(sample(c(-1,1), nsteps, repl=TRUE)))) } as.matrix(lapply(split(rt, factor(rt)), function (x) { length(x)/nruns })); } > lst $age [1] 40 $height [1] 5.9 $colors [1] "red" "blue" > lst$colors [1] "red" "blue" > lst$colors[1] [1] "red" > lst$colors[2] [1] "blue" > typeof(x) [1] "double" > typeof(lst) [1] "list" > prob.of.ret() [,1] 2 0.475 4 0.135 6 0.063 8 0.044 10 0.035 12 0.022 14 0.013 16 0.01 18 0.008 20 0.004 22 0.009 24 0.005 26 0.005 28 0.004 30 0.005 32 0.004 34 0.006 38 0.003 40 0.003 42 0.001 44 0.002 46 0.001 48 0.005 50 0.005 52 0.007 56 0.001 58 0.001 60 0.002 62 0.002 64 0.003 66 0.003 68 0.004 70 0.001 72 0.003 78 0.001 80 0.001 82 0.001 84 0.001 90 0.003 94 0.002 98 0.001 100 0.001 > prob.of.ret(nsteps = 10) [,1] 2 0.504 4 0.114 6 0.075 8 0.037 10 0.024 > cumsum(c(1, 2, 3)) [1] 6 > cumsum(c(1, 2, 3)) [1] 1 3 6 > cumsum(c(1, 2, 3, 7, 1)) [1] 1 3 6 13 14 > sample(c(-1,1), 10) [1] -1 1 > sample(c(-1,1), 10, repl = TRUE) Error in sample(length(x), size, replace, prob) : can't take a sample larger than the population when replace = FALSE > sample(c(-1,1), 10, repl = TRUE) [1] 1 -1 1 1 1 -1 -1 1 -1 -1 > match(0, c(1, 0, 2, 0, 3)) Error: syntax error > match(0, c(1, 0, 2, 0, 3)) [1] 2 > intersect <- function(x, y) y[match(x, y, nomatch = 0)] > intersect(c(1,2,3), c(2, 3, 7)) [1] 2 3 > match(0, c(1, 7, 2, 8, 3)) [1] 2 > 0 %in% c(1, 0, 2, 0, 3) [1] TRUE > match(0, c(1, 7, 2, 8, 3), nomatch = "yuk!") [1] NA > match(0, c(1, 7, 2, 8, 3), nomatch = 7) [1] NA > match(0, c(1, 7, 2, 8, 3), nomatch = 1000) [1] 7 > match(0, c(1, 7, 2, 8, 3), nomatch = 1000) [1] 1000 >