next up previous contents
Next: Subsetting Matrices Up: Splus/R Previous: Random Data   Contents

Maintaining your S/R Objects

Thus far we have created several objects, but we have not talked about deleting, listing, nor any operations on the vectors post-creation. In this section, we will do just that. If you have a background in unix or linux, many of these commands will be familiar.

First, using the ls() command will list the objects that are in your current working S/R directory. Doing this here, we see the vectors that were created previously in this session.

> ls()
 [1] "mm"    "names" "tm"    "x"     "x.1"   "x.2"   "x.3"   "xx"    "xy"   
[10] "y"
Note that using the command objects() will produce the same output as
tt ls().

Deleting objects is done using the remove command, or rm(object.name). If we want to delete the object x.3 from our current directory, we issue the command

> rm(x.3)
> ls()
[1] "names" "x"     "x.1"   "x.2"   "xx"    "xy"    "y"
and then list to see that it has been deleted. Issuing the rm() command on an object that does not exist will produce warning.
> rm(x.3)
Warning message: 
remove: variable "x.3" was not found

The ``subsetting'' capabilities are an extremely powerful set of tools and should be mentioned here. In order to understand this concept, we first need to understand the indexing of S or R vectors and later, matrices. From a mathematical viewpoint, the indexing scheme makes sense, however it is not consistent with some lower-level programming languanges, $ e.g.$ C or C++. Suppose that we have a vector, v, having $ n$ elements. In a vector algebra class, we might denote v as $ \boldsymbol{v} = (v_1, v_2, \ldots, v_n)$ where the subscripts represent the index of the particular element. In S-plus or R, we will think of the vector v as v = (v[1],v[2],...,v[n]). We will illustrate this concept with the vector consisting of 20 random binomial variates having probability of success .5 and 10 trials. First, define the vector

 > bn <- rbinom(20,size=10,prob=.5)
> bn
 [1] 4 2 6 7 3 5 6 6 7 4 6 3 5 5 3 3 5 4 3 6
and notice that bn[1], bn[8], and bn[19] are given by
> bn[1]
[1] 4
> bn[8]
[1] 6
> bn[19]
[1] 3
respectively. Now we need to point out that 1,8, and 19 are actually 1-dimensional vectors and that any vector containing the numbers from $ 1,2,\ldots,20$ can be used as the indexing vector. For instance, to obtain the $ 1^{st}$, $ 8^{th}$, and $ 19^{th}$ elements of bn, we could have used the concatnenate command
> bn[c(1,8,19)]
[1] 4 6 3
or for the $ 5^{th} - 10^{th}$ elements, the sequence command.
> bn[seq(5,10,by=1)]
[1] 3 5 6 6 7 4
We can also using logical vectors as the indexing vector. If the logical vector's element evaluates to TRUE, that particular element becomes an element of the subset. Consider the logical vector created by testing whether or not the elements are greater than 4
> bn > 4
 [1] FALSE FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE
[13]  TRUE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE
Therefore, if we want the elements of bn which are greater than 4, we simply place this vector bn > 4 inside the brackets of bn
> bn[bn > 4]
 [1] 6 7 5 6 6 7 6 5 5 5 6
and the appropriate elements are obtained.



Subsections
next up previous contents
Next: Subsetting Matrices Up: Splus/R Previous: Random Data   Contents
Computer Support Group 2002-10-07