- Splus stores data in vectors, which are a collection of
numbers or words. Here's how you can make a vector of
5 numbers: 1 3 7 8 9:
> 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.
-
To look at x, type:
> x
[1] 1 3 7 8 9
The 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] 7
Splus 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 9
gives 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
- You can also get a subset of a vector by removing certain
elements. x[-i] is x with its ith element removed:
> x[-1]
[1] 3 7 8 9
> x[c(-2,-4)]
[1] 1 7 9
-
Another way to generate values is the seq() function. Here
are some examples:
> 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
-
Another way to generate data is thru a random number generator.
In Splus there are many random number generating functions.
Here are some examples:
> 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.05631129
In the Splus commands above, the # sign means comments - anything
written after that is not digested by Splus.
-
Vectors can also be created from other vectors. For example
> 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
-
Finally, vectors can be of strings (words) or logical (T/F) values:
> c("yes","no","no","yes","yes")
[1] "yes" "no" "no" "yes" "yes"
> c(T,T,T,F,T)
[1] T T T F T
-
Logical vectors are particularly useful for grabbing a subset
of a vector:
> ok_x>5
[1] F F T T T
> x[ok]
[1] 7 8 9
> x[x>5]
[1] 7 8 9
-
Plots are simple using vectors, for example
> 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")
-
Before going farther, you can tell Splus how many plots to
put on a page using the par command. The command below
specifies 2 rows and 2 columns of plots per page:
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")
-
There are a number of additional parameters you can use in
the plot() function. See the "Traditional Graphics" section
of the Splus programming guide for more detailed info on the
plot function. Some examples are:
> 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
-
You can add points to an existing graph using the points() command:
> 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+.5
and you can also add lines using lines():
> lines(x+.5,y+.5) # add points x+.5 and y+.5
-
Other plots are also available for numeric vectors
> rvals_rnorm(300,mean=20,sd=10)
> hist(rvals,main="histogram")
> boxplot(rvals,main="boxplot")
- matricies can be made by combining columns using cbind():
> 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
- or by combining rows using rbind():
> 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
-
You can access elements of a matrix:
> mat[2,3]
y
7.5
-
You can access a column of a matrix
> mat[,2]
x y z
3 6.25 9.25
-
You can access a row of a matrix
> mat[1,]
[1] 1 3 7 8 9
-
You can use the matrix command to build a matrix:
> 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