R version 2.9.2 (2009-08-24) 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') > n <- 100 > path=cumsum(sample(c(1,-1), n, replace=T)); > plot(path) > n <- 100 > path=cumsum(sample(c(1,-1), n, replace=T)); > plot(path, type='l') > ## Plot the winnings of a simple H&T game > ## where you win or lose 1 penny each time > n <- 1000 > path=cumsum(sample(c(1,-1), n, replace=T)); > plot(path, type='l') > ?sample > any(c(1,2,3)>5) [1] FALSE > ## Plot the winnings of a simple H&T game > ## where you win or lose 1 penny each time > n <- 1000 > winnings <- cumsum(sample(c(1,-1), n, replace=T)); > plot(winnings, type='l') > > > ## Count the number of games during which > ## the winnings exceed 5 pennies > ngames->100 Error in 100 <- ngames : invalid (do_set) left-hand side to assignment > goalAchieved= vector(ngames) Error in vector(ngames) : object 'ngames' not found > for k in 1:ngames { Error: unexpected symbol in "for k" > winnings <- cumsum(sample(c(1,-1), n, replace=T)); > goalAchieved[k]=any(winnings>=5) Error in goalAchieved[k] = any(winnings >= 5) : object 'goalAchieved' not found > } Error: unexpected '}' in "}" > timesGoalAchieved <- length(which(goalAchieved)) Error in which(goalAchieved) : object 'goalAchieved' not found > goalAchieved <- vector(ngames) Error in vector(ngames) : object 'ngames' not found > ngames <- 100 > ngames <- 100 > goalAchieved <- vector(ngames) Error in vector(ngames) : vector: cannot make a vector of mode '100'. > for k in 1:ngames { Error: unexpected symbol in "for k" > winnings <- cumsum(sample(c(1,-1), n, replace=T)); > goalAchieved[k]=any(winnings>=5) Error in goalAchieved[k] = any(winnings >= 5) : object 'goalAchieved' not found > } Error: unexpected '}' in "}" > timesGoalAchieved <- length(which(goalAchieved)) Error in which(goalAchieved) : object 'goalAchieved' not found > ngames <- 100 > goalAchieved <- vector(ngames) Error in vector(ngames) : vector: cannot make a vector of mode '100'. > for k in 1:ngames { Error: unexpected symbol in "for k" > winnings <- cumsum(sample(c(1,-1), n, replace=T)) > goalAchieved[k] <- any(winnings>=5) Error in goalAchieved[k] <- any(winnings >= 5) : object 'goalAchieved' not found > } Error: unexpected '}' in "}" > timesGoalAchieved <- length(which(goalAchieved)) Error in which(goalAchieved) : object 'goalAchieved' not found > ?vector > ngames <- 100 > goalAchieved <- vector(length=ngames) > for k in 1:ngames { Error: unexpected symbol in "for k" > winnings <- cumsum(sample(c(1,-1), n, replace=T)) > goalAchieved[k] <- any(winnings>=5) Error in goalAchieved[k] <- any(winnings >= 5) : object 'k' not found > } Error: unexpected '}' in "}" > timesGoalAchieved <- length(which(goalAchieved)) > ngames <- 100 > goalAchieved <- vector(length=ngames) > for(k in 1:ngames) { + winnings <- cumsum(sample(c(1,-1), n, replace=T)) + goalAchieved[k] <- any(winnings>=5) + } > timesGoalAchieved <- length(which(goalAchieved)) > timesGoalAchieved [1] 88 > ## Count the number of games during which > ## the winnings exceed 5 pennies > ngames <- 100 > goalAchieved <- vector(length=ngames) > for(k in 1:ngames) { + winnings <- cumsum(sample(c(1,-1), n, replace=T)) + goalAchieved[k] <- any(winnings>=5) + } > timesGoalAchieved <- length(which(goalAchieved)) > probGoalAchieved <- timesGoalAchieved / ngames > probGoalAchieved [1] 0.9 > n <- 1000 > ev <- sample(c(1,-1), n, replace=T) > ev [1] 1 1 -1 -1 -1 1 1 1 -1 -1 1 1 -1 1 1 1 1 -1 -1 1 -1 1 1 1 [25] -1 1 1 1 -1 1 -1 -1 1 1 -1 -1 -1 -1 1 -1 -1 -1 -1 -1 1 -1 1 -1 [49] -1 -1 1 1 1 -1 -1 -1 -1 1 -1 -1 1 1 1 -1 1 1 1 -1 -1 1 1 -1 [73] 1 -1 -1 1 -1 1 -1 1 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-1 -1 1 1 1 1 1 -1 1 -1 -1 [865] 1 -1 1 1 -1 -1 1 1 1 -1 1 -1 -1 1 1 -1 1 -1 -1 -1 1 1 -1 -1 [889] 1 1 1 1 1 1 -1 -1 -1 1 -1 -1 -1 1 1 -1 1 1 1 -1 1 1 1 1 [913] -1 1 1 -1 1 1 -1 -1 -1 -1 -1 1 -1 -1 1 1 -1 1 -1 -1 1 1 -1 -1 [937] 1 1 1 1 -1 1 1 1 -1 1 1 -1 -1 1 1 -1 1 -1 1 1 1 1 1 -1 [961] 1 1 1 -1 1 -1 1 1 1 -1 1 -1 -1 1 1 -1 1 1 1 -1 1 -1 -1 1 [985] -1 1 1 -1 -1 -1 -1 -1 1 1 1 1 1 1 -1 1 > v <- c(1,2,3,2,1) > cumsum(v) [1] 1 3 6 8 9 > > v [1] 1 2 3 2 1 > v>2 [1] FALSE FALSE TRUE FALSE FALSE > [1] FALSE FALSE TRUE FALSE FALSE Warning message: In v > c(1, 2) : longer object length is not a multiple of shorter object length > z=v>2 > z [1] FALSE FALSE TRUE FALSE FALSE > any(z) [1] TRUE > timesGoalAchieved <- length(which(goalAchieved)) > goalAchieved [1] TRUE TRUE TRUE TRUE FALSE TRUE TRUE FALSE TRUE TRUE TRUE FALSE [13] TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE [25] TRUE TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE [37] TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE [49] TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE [61] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [73] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [85] TRUE TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE [97] TRUE TRUE TRUE TRUE > which(goalAchieved) [1] 1 2 3 4 6 7 9 10 11 13 14 15 17 18 19 20 21 22 23 [20] 25 26 27 28 29 30 32 33 34 35 36 37 38 39 40 41 43 44 45 [39] 46 47 48 49 50 51 52 53 54 57 58 59 60 61 62 63 64 65 66 [58] 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 [77] 86 87 88 89 90 92 93 94 95 96 97 98 99 100 > probGoalAchieved <- timesGoalAchieved / ngames > probGoalAchieved [1] 0.9 > source("script.R") > probGoalAchieved [1] 0.99 > v [1] 1 2 3 2 1 >