# To enter Splus type Splus # You could add the option -e to add command editing # 1. We study operators: # + , - , * , / # %/% integer divide # %% modulus operator # ^ Exponentiation # - unary minus # Precedence ^ * / + - 7 %% 3 + 4%/% 3 7 %% 3^2 # Sequence operator 1:5 27:3 1:5 + 1:5 # vector arithmetic 1:5 + 3 # precedence 2^(1:5) # c function combines vectors c(1,2*4,4, 10:12) c("dog","cat","mouse") # Exercise 1: Produce the sequence # 2 3 4 5 1 2 3 5 1 2 3 4 5... # Exercise 2: Produce the vector # "Monday", "Tuesday", "Wednessday" ##### The assignment operator '<-' ##### primes <- c(1,2,3,5,7,11) greeting <- "Hello World" # S data Objects are saved in .Data # Data in S are of mode: logical,integer # real , character. # Common S functions:abs, cos, exp,log, # log10, round, sin, sqrt # len, max,mean,median, min,prod,sum,var # range, sample, sort, seq, runif,rnorm ### Random numbers #### runif(10) x <- rnorm(10) # Ex compute s-dev of x sqrt(var(x)) # function seq seq( len=12, to=27, by=2) # to stop a computation ^C # to get help help(median) # or use the ? character: ?median # options options(width=132,length=200) # create a file with a data set in your favourite editor. I use emacs # could be an array of numbers 6 by 4 separated by spaces !emacs myfile !cat myfile # creating datasets x <- scan() y <- scan("myfile") # creating data frames y <- read.table("myfile") # my data objects ls() objects(pat="x") # Important to remove unused rm(x) x <- sin(y) ### Graphics ##### # Devices: printer(), tek4014(), X11(), postscript() printer(80,28) par(mar=c(2.2,2.2,1,1)) plot(y) #simple scatter plot plot(y,type="l") #connected lines # log-log plot x <- y$V1 y <- y$V2 plot(x,y,log="xy") show() # do not plot, then # use text to label each point from 1 to n plot(x,y,type="n");text(x,y,seq(x)) # Using a better device: X11() plot(corn.rain,corn.yield) abline(20,1) # addline y = a+b x. Pick the values of a and b yourself abline(h=30); abline(v=12) title(sub="This is corn data") # Sending a graph to a postscript file postscript() plot(corn.rain,corn.yield) title(sub="This is corn data") graphics.off() #####Matrices##### x <- matrix(scan("myfile"),3,4,byrow=T) x <- matrix(sqrt(1:12),4,3)-3 x x[ 3,1:2] rownum <- 1:4 # get odd rows rownum %% 2 x[rownum %%2 ==1 , ] x[ -1,-c(1,3)] nrow(x) ncol(x) y <- 1:3 x%*%y y <- rnorm(4) solve(t(x) %*% x) %*% (t(x) %*% y) x <- matrix(rnorm(18),6,3) y <- x%*% c(1,2,3) + rnorm(6)/4 solve(t(x) %*% x) %*% (t(x) %*% y) lsfit(x,y,int=F) ############ Data Structures##### simple <- list(name= "Simple Stats", mean= mean(x),sdev=sqrt(var(c(x)))) simple$name ##Create a copy of "myfile" with variable names in the first row. !emacs myfile x <- read.table("myfile1") names(x) x$z <- x$x + x$y -3 names(x) ##### LOGICAL OPERATORS ####### # > , >= , < , <= , == , != , & , | or ! Not u <- x > 0 u print(!u) x[u] u <- log(x) is.na(u) u$z[is.na(u$z)] <- 0 # You can put some or all these instructions into your own file "program" # and excecute them all at once with the instruction: # source("program") ########## CONDITIONAL ######### u <- sum(x) if( u < 0) { x <- -x } ######### LOOPS ############ z <- matrix(rnorm(200),20,10) mean.samp <- NULL for(i in 1:10) { mean.samp[i] <- mean(z[,i]) } stem(mean.samp) ############# FUNCTIONS ######## fourmom <- function(x) { m1 <- c(mean(x)) m2 <- mean(x^2) m3 <- mean(x^3) m4 <- mean(x^4) list(m1=m1,m2=m2,m3=m3,m4=m4) } fourmom(x)