Goto Lab: | #0 | #1 |
> x_c(1,3,7,8,9)The above notation means type "x_c(1,3,7,8,9)" at the Splus command line prompt which looks like ">" - you don't type that part. The underscore characther "_" means "=". So here the above command reads "put x equal to a vector with elements 1, 3, 7, 8, and 9 in that order.
> x [1] 1 3 7 8 9The second line is what Splus returns. Here it returns the five values of x. The [1] tells us the line starts with the 1st element of x. This is important when looking at long vectors. We can look at a single element of x by typing:
> x[3] [1] 7Splus returns the number 3 which is a vector of length 1. We can also look at pieces of x; for example:
> x[c(1,4,5)] [1] 1 8 9gives the 1st, 4th and 5th elements of x. We can even go a little crazy here:
> ivals_c(1,5,1,5,1,5,1,5) > x[ivals] [1] 1 9 1 9 1 9 1 9
> x[-1] [1] 3 7 8 9 > x[c(-2,-4)] [1] 1 7 9
> seq(2,20,by=2) [1] 2 4 6 8 10 12 14 16 18 20 > seq(0,by=.1,length=5) [1] 0.0 0.1 0.2 0.3 0.4 0.5 > y_seq(5,10,length=5) > y [1] 5.00 6.25 7.50 8.75 10.00
> rnorm(5,mean=69,sd=3) # random normals with mean=69 and sd=3 [1] 69.58425 67.08999 71.01311 69.05869 75.45862 > runif(3,min=0,max=1) # 3 uniform draws between 0 and 1 [1] 0.30378091 0.82247583 0.05631129In the Splus commands above, the # sign means comments - anything written after that is not digested by Splus.
> z_x+y > z [1] 6.00 9.25 14.50 16.75 19.00 > u_c(x,y) > u [1] 1.00 3.00 7.00 8.00 9.00 5.00 6.25 [8] 7.50 8.75 10.00
> c("yes","no","no","yes","yes") [1] "yes" "no" "no" "yes" "yes" > c(T,T,T,F,T) [1] T T T F T
> ok_x>5 [1] F F T T T > x[ok] [1] 7 8 9 > x[x>5] [1] 7 8 9
> mean(x) [1] 5.6 > median(x) [1] 7 > summary(rnorm(100,mean=50,sd=10)) Min. 1st Qu. Median Mean 3rd Qu. Max. 25.14 43.89 50.83 50.48 57.63 74.65 > sort(x) [1] 1 3 7 8 9
> plot(x,y)gives a plot of the x vs y values. You can change the plotting characters using pch:
> plot(x,y,pch=2) > plot(x,y,pch=3) > plot(x,y,pch="X")
par(mfrow=c(2,2))Now 4 plots fit on a single page:
> plot(x,y,pch=1) > plot(x,y,pch=2) > plot(x,y,pch=3) > plot(x,y,pch="X")
> plot(x,y,type="l") # connect the points with lines > plot(x,y,type="b",xlab="x label",ylab="y label",main="with labels") > plot(x,y,type="l",lty=2) # dotted line > plot(rnorm(100),rnorm(100),pch=".",xlim=c(-3,3),ylim=c(-3,3),pty="s") # a big cloud of standard normals; x and y ranges are preset to +/-3; # pty="s" tells Splus to make a square plot
> plot(x,y,ylim=c(5,11),xlim=c(0,10)) # plot of x and y > points(x+.5,y+.5,pch=2) # add points x+.5 and y+.5and you can also add lines using lines():
> lines(x+.5,y+.5) # add points x+.5 and y+.5
> rvals_rnorm(300,mean=20,sd=10) > hist(rvals,main="histogram") > boxplot(rvals,main="boxplot")
> mat_cbind(x,y,z) > mat x y z [1,] 1 5.00 6.00 [2,] 3 6.25 9.25 [3,] 7 7.50 14.50 [4,] 8 8.75 16.75 [5,] 9 10.00 19.00
> mat_rbind(x,y,z) > mat [,1] [,2] [,3] [,4] [,5] x 1 3.00 7.0 8.00 9 y 5 6.25 7.5 8.75 10 z 6 9.25 14.5 16.75 19
> mat[2,3] y 7.5
> mat[,2] x y z 3 6.25 9.25
> mat[1,] [1] 1 3 7 8 9
> mat_matrix(seq(0,by=5,length=12),ncol=3,nrow=4) > mat [,1] [,2] [,3] [1,] 0 20 40 [2,] 5 25 45 [3,] 10 30 50 [4,] 15 35 55
> ymat_cbind(rnorm(10,mean=0,sd=.2),rnorm(10,mean=2,sd=.3), rnorm(10,mean=5,sd=.4)) > x10_seq(1,10,by=1) > xmat_cbind(x10,x10,x10) > matplot(xmat,ymat) # points only > matplot(xmat,ymat,type="l") # lines only > matplot(xmat,ymat,type="b") # points and lines
> df1_data.frame(group=c("A","A","B","B"),score=c(10,7,9,3)) > df1 group score 1 A 10 2 A 7 3 B 9 4 B 3
> df1$score [1] 10 7 9 3 > df1[,2] [1] 10 7 9 3
> plot(c(1,2,3,4),df1$score,type="b",xlab="index",ylab="score") > mean(df1$score) # mean of the entries of df1$score [1] 7.25 > var(df1$score) # variance of the entries of df1$score [1] 9.583333