R version 2.10.0 (2009-10-26) Copyright (C) 2009 The R Foundation for Statistical Computing 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. Natural language support but running in an English locale 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. > options(STERM='iESS', editor='emacsclient') > ?sprinf > ?sprintf > printf("%s is %f feet tall\n", "Sven", 7.1) [1] "Sven is 7.100000 feet tall\n" > sprintf("%s is %f feet tall\n", "Sven", 7.1) Error: could not find function "printf" > sprintf("%s is %f feet tall\n", "Sven", 7.1) [1] "Sven is 7.100000 feet tall\n" > ?write > write(sprintf("%s is %f feet tall\n", "Sven", 7.1),file="sven") > ?sprintf > sprintf("%%"); [1] "%" > sprintf("%%") [1] "%" > ls() character(0) > x <- "Jane" > ls() [1] "x" > jane <- 1 > ls() [1] "jane" "x" > 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))) && ll != length(grep("]", pattern, fixed = TRUE))) { if (pattern == "[") { pattern <- "\\[" warning("replaced regular expression pattern '[' by '\\\\['") } else if (length(grep("[^\\\\]\\[<-", pattern))) { pattern <- sub("\\[<-", "\\\\\\[<-", pattern) warning("replaced '[<-' by '\\\\[<-' in regular expression pattern") } } grep(pattern, all.names, value = TRUE) } else all.names } > ## Functions > add2 <- function(x)x+2 > add2(3) [1] 5 > add2(5) [1] 7 > add2(jane) Error in x + 2 : non-numeric argument to binary operator > add2(jane) [1] 3 > jane [1] 1 > add2 <- function(x){ x+2; } > add2 <- function(x){ add2 <- function(x){ + + x+2; + } > foo <- function(x) { + "jane"+x; + } > foo function(x) { "jane"+x; } > foo(2) Error in "jane" + x : non-numeric argument to binary operator > foo("x") Error in "jane" + x : non-numeric argument to binary operator > x=2 > x==2 [1] TRUE > 2->x > x [1] 2 > floor(3.4) [1] 3 > floor(-3.4) [1] -4 > ## An advanced function example > f <- function(x) {if(x%%2==0) x%/%2 else 3*x+1;} #Function in Collatz problem > g <- function(x) if(x==1)T else g(f(x)) #Is it always T? > g(100) [1] TRUE > h(7) Error: could not find function "h" > ## An advanced function example > f <- function(x) {if(x%%2==0) x%/%2 else 3*x+1;} #Function in Collatz problem > g <- function(x) if(x==1)T else g(f(x)) #Is it always T? > g(100) [1] TRUE > h <- function(x) {print(x); if(x==1)T else h(f(x))} #Like g, but prints x > h(7) [1] 7 [1] 22 [1] 11 [1] 34 [1] 17 [1] 52 [1] 26 [1] 13 [1] 40 [1] 20 [1] 10 [1] 5 [1] 16 [1] 8 [1] 4 [1] 2 [1] 1 [1] TRUE > h(134123) [1] 134123 [1] 402370 [1] 201185 [1] 603556 [1] 301778 [1] 150889 [1] 452668 [1] 226334 [1] 113167 [1] 339502 [1] 169751 [1] 509254 [1] 254627 [1] 763882 [1] 381941 [1] 1145824 [1] 572912 [1] 286456 [1] 143228 [1] 71614 [1] 35807 [1] 107422 [1] 53711 [1] 161134 [1] 80567 [1] 241702 [1] 120851 [1] 362554 [1] 181277 [1] 543832 [1] 271916 [1] 135958 [1] 67979 [1] 203938 [1] 101969 [1] 305908 [1] 152954 [1] 76477 [1] 229432 [1] 114716 [1] 57358 [1] 28679 [1] 86038 [1] 43019 [1] 129058 [1] 64529 [1] 193588 [1] 96794 [1] 48397 [1] 145192 [1] 72596 [1] 36298 [1] 18149 [1] 54448 [1] 27224 [1] 13612 [1] 6806 [1] 3403 [1] 10210 [1] 5105 [1] 15316 [1] 7658 [1] 3829 [1] 11488 [1] 5744 [1] 2872 [1] 1436 [1] 718 [1] 359 [1] 1078 [1] 539 [1] 1618 [1] 809 [1] 2428 [1] 1214 [1] 607 [1] 1822 [1] 911 [1] 2734 [1] 1367 [1] 4102 [1] 2051 [1] 6154 [1] 3077 [1] 9232 [1] 4616 [1] 2308 [1] 1154 [1] 577 [1] 1732 [1] 866 [1] 433 [1] 1300 [1] 650 [1] 325 [1] 976 [1] 488 [1] 244 [1] 122 [1] 61 [1] 184 [1] 92 [1] 46 [1] 23 [1] 70 [1] 35 [1] 106 [1] 53 [1] 160 [1] 80 [1] 40 [1] 20 [1] 10 [1] 5 [1] 16 [1] 8 [1] 4 [1] 2 [1] 1 [1] TRUE > h(1341234141234) [1] 1.341234e+12 [1] 670617070617 [1] 2.011851e+12 [1] 1.005926e+12 [1] 502962802963 [1] 1.508888e+12 [1] 754444204445 [1] 2.263333e+12 [1] 1.131666e+12 [1] 565833153334 [1] 282916576667 [1] 848749730002 [1] 424374865001 [1] 1.273125e+12 [1] 636562297502 [1] 318281148751 [1] 954843446254 [1] 477421723127 [1] 1.432265e+12 [1] 716132584691 [1] 2.148398e+12 [1] 1.074199e+12 [1] 3.222597e+12 [1] 1.611298e+12 [1] 805649157778 [1] 402824578889 [1] 1.208474e+12 [1] 604236868334 [1] 302118434167 [1] 906355302502 [1] 453177651251 [1] 1.359533e+12 [1] 679766476877 [1] 2.039299e+12 [1] 1.019650e+12 [1] 509824857658 [1] 254912428829 [1] 764737286488 [1] 382368643244 [1] 191184321622 [1] 95592160811 [1] 286776482434 [1] 143388241217 [1] 430164723652 [1] 215082361826 [1] 107541180913 [1] 322623542740 [1] 161311771370 [1] 80655885685 [1] 241967657056 [1] 120983828528 [1] 60491914264 [1] 30245957132 [1] 15122978566 [1] 7561489283 [1] 22684467850 [1] 11342233925 [1] 34026701776 [1] 17013350888 [1] 8506675444 [1] 4253337722 [1] 2126668861 [1] 6380006584 [1] 3190003292 [1] 1595001646 [1] 797500823 [1] 2392502470 [1] 1196251235 [1] 3588753706 [1] 1794376853 [1] 5383130560 [1] 2691565280 [1] 1345782640 [1] 672891320 [1] 336445660 [1] 168222830 [1] 84111415 [1] 252334246 [1] 126167123 [1] 378501370 [1] 189250685 [1] 567752056 [1] 283876028 [1] 141938014 [1] 70969007 [1] 212907022 [1] 106453511 [1] 319360534 [1] 159680267 [1] 479040802 [1] 239520401 [1] 718561204 [1] 359280602 [1] 179640301 [1] 538920904 [1] 269460452 [1] 134730226 [1] 67365113 [1] 202095340 [1] 101047670 [1] 50523835 [1] 151571506 [1] 75785753 [1] 227357260 [1] 113678630 [1] 56839315 [1] 170517946 [1] 85258973 [1] 255776920 [1] 127888460 [1] 63944230 [1] 31972115 [1] 95916346 [1] 47958173 [1] 143874520 [1] 71937260 [1] 35968630 [1] 17984315 [1] 53952946 [1] 26976473 [1] 80929420 [1] 40464710 [1] 20232355 [1] 60697066 [1] 30348533 [1] 91045600 [1] 45522800 [1] 22761400 [1] 11380700 [1] 5690350 [1] 2845175 [1] 8535526 [1] 4267763 [1] 12803290 [1] 6401645 [1] 19204936 [1] 9602468 [1] 4801234 [1] 2400617 [1] 7201852 [1] 3600926 [1] 1800463 [1] 5401390 [1] 2700695 [1] 8102086 [1] 4051043 [1] 12153130 [1] 6076565 [1] 18229696 [1] 9114848 [1] 4557424 [1] 2278712 [1] 1139356 [1] 569678 [1] 284839 [1] 854518 [1] 427259 [1] 1281778 [1] 640889 [1] 1922668 [1] 961334 [1] 480667 [1] 1442002 [1] 721001 [1] 2163004 [1] 1081502 [1] 540751 [1] 1622254 [1] 811127 [1] 2433382 [1] 1216691 [1] 3650074 [1] 1825037 [1] 5475112 [1] 2737556 [1] 1368778 [1] 684389 [1] 2053168 [1] 1026584 [1] 513292 [1] 256646 [1] 128323 [1] 384970 [1] 192485 [1] 577456 [1] 288728 [1] 144364 [1] 72182 [1] 36091 [1] 108274 [1] 54137 [1] 162412 [1] 81206 [1] 40603 [1] 121810 [1] 60905 [1] 182716 [1] 91358 [1] 45679 [1] 137038 [1] 68519 [1] 205558 [1] 102779 [1] 308338 [1] 154169 [1] 462508 [1] 231254 [1] 115627 [1] 346882 [1] 173441 [1] 520324 [1] 260162 [1] 130081 [1] 390244 [1] 195122 [1] 97561 [1] 292684 [1] 146342 [1] 73171 [1] 219514 [1] 109757 [1] 329272 [1] 164636 [1] 82318 [1] 41159 [1] 123478 [1] 61739 [1] 185218 [1] 92609 [1] 277828 [1] 138914 [1] 69457 [1] 208372 [1] 104186 [1] 52093 [1] 156280 [1] 78140 [1] 39070 [1] 19535 [1] 58606 [1] 29303 [1] 87910 [1] 43955 [1] 131866 [1] 65933 [1] 197800 [1] 98900 [1] 49450 [1] 24725 [1] 74176 [1] 37088 [1] 18544 [1] 9272 [1] 4636 [1] 2318 [1] 1159 [1] 3478 [1] 1739 [1] 5218 [1] 2609 [1] 7828 [1] 3914 [1] 1957 [1] 5872 [1] 2936 [1] 1468 [1] 734 [1] 367 [1] 1102 [1] 551 [1] 1654 [1] 827 [1] 2482 [1] 1241 [1] 3724 [1] 1862 [1] 931 [1] 2794 [1] 1397 [1] 4192 [1] 2096 [1] 1048 [1] 524 [1] 262 [1] 131 [1] 394 [1] 197 [1] 592 [1] 296 [1] 148 [1] 74 [1] 37 [1] 112 [1] 56 [1] 28 [1] 14 [1] 7 [1] 22 [1] 11 [1] 34 [1] 17 [1] 52 [1] 26 [1] 13 [1] 40 [1] 20 [1] 10 [1] 5 [1] 16 [1] 8 [1] 4 [1] 2 [1] 1 [1] TRUE > ## Generate Good and Defective items > n <- 100 #Number of samples > p <- c(.9,.1) #Probabilities > x <- sample(c("G","D"),n,replace=T,prob=p) #Sample(outcome) > which(x=="D") #Show indices of defective items [1] 1 10 17 18 20 24 31 64 73 75 > length(which(x=="D")) #How many are defective? [1] 10 > sample(c(1,2),5) Error in sample(c(1, 2), 5) : cannot take a sample larger than the population when 'replace = FALSE' > sample(c(1,2,3,4,5,6),5) [1] 2 1 3 5 6 > sample(c(1,2,3,4,5,6),5,replace=F) [1] 2 5 6 3 1 > sample(c(1,2),5,replace=F) Error in sample(c(1, 2), 5, replace = F) : cannot take a sample larger than the population when 'replace = FALSE' > sample(c(1,2),5,replace=T) [1] 1 1 2 2 1 > sample(c(1,2),5,replace=T) [1] 2 2 2 2 1 > sample(c(1,2),5,replace=T) [1] 2 2 2 1 1 > sample(c("H","T"),5,replace=T) [1] "T" "H" "T" "T" "T" > sample(c("H","T"),50,replace=T)b Error: unexpected symbol in "sample(c("H","T"),50,replace=T)b" > sample(c("H","T"),50,replace=T) [1] "T" "T" "H" "T" "T" "T" "H" "H" "T" "T" "H" "T" "H" "H" "H" "H" "H" "T" "H" [20] "H" "H" "T" "H" "H" "T" "T" "T" "T" "H" "H" "T" "H" "T" "T" "T" "H" "T" "H" [39] "T" "H" "H" "H" "T" "T" "T" "T" "H" "T" "H" "H" > x <- sample(c("H","T"),50,replace=T) > x [1] "T" "T" "H" "T" "H" "T" "T" "T" "H" "H" "H" "H" "H" "T" "T" "T" "H" "H" "T" [20] "T" "T" "H" "T" "H" "H" "H" "T" "H" "T" "T" "T" "H" "T" "H" "T" "H" "T" "T" [39] "T" "T" "H" "T" "T" "T" "H" "T" "H" "H" "H" "H" > which(x=="H") [1] 3 5 9 10 11 12 13 17 18 22 24 25 26 28 32 34 36 41 45 47 48 49 50 > length(which(x=="H")) [1] 23 > 1==2 [1] FALSE > 1==1 [1] TRUE > x=="H" [1] FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE TRUE TRUE TRUE TRUE [13] TRUE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE TRUE FALSE TRUE [25] TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE FALSE TRUE FALSE TRUE [37] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE TRUE TRUE [49] TRUE TRUE > x [1] "T" "T" "H" "T" "H" "T" "T" "T" "H" "H" "H" "H" "H" "T" "T" "T" "H" "H" "T" [20] "T" "T" "H" "T" "H" "H" "H" "T" "H" "T" "T" "T" "H" "T" "H" "T" "H" "T" "T" [39] "T" "T" "H" "T" "T" "T" "H" "T" "H" "H" "H" "H" > which(c(T,F,T,T)) [1] 1 3 4 > which(c(1,2,3)) Error in which(c(1, 2, 3)) : argument to 'which' is not logical > ## In this example, we generate random driving directions. > ## At every intersetion we can turn left, right or go straight. > > ## Driving directions > x <- c("L","R","S") > ## The number of turns > n <- 4 > ## Generate choice of direction > ## This generages arbitrary n-tuples. > path <- sample(x, n, replace=TRUE) > > ## Print the generated path > print(path) [1] "L" "S" "R" "L" >