s-news
[Top] [All Lists]

[no subject]

It's a good idea to avoid naming an S-PLUS data object 'df', since that
will mask the built-in S-PLUS 'df()' function, which returns the density
of the F distribution.


Here is a trio of examples using subscripting.  The first selects from
a vector; the second selects from the second column of a data frame. 

The third example returns a logical vector whose elements are T wherever
the corresponding element of the column is less than zero, and F elsewhere.  


vector
``````

The following commands create and display a repeatable vector named 'daj':
 
        > set.seed(1)
 
        > daj <- round(rnorm(35), 2)
 
        > daj
         [1] -0.79  0.79 -0.89  0.11  1.37  1.42  1.17 -0.53  0.92 -0.58
        [11] -0.09 -0.96 -0.27 -1.34  0.76 -0.56  0.19 -0.93 -0.18 -1.58
        [21]  0.88 -0.22  0.06 -0.80 -0.49 -0.38 -2.17 -0.27 -0.66  1.43
        [31] -0.59  0.18 -0.99  1.85  1.34


The following commands create and display those elements of 'daj'
whose values are less than zero: 
 
        > test <- daj[daj<0]
 
        > test
         [1] -0.79 -0.89 -0.53 -0.58 -0.09 -0.96 -0.27 -1.34 -0.56 -0.93
        [11] -0.18 -1.58 -0.22 -0.80 -0.49 -0.38 -2.17 -0.27 -0.66 -0.59
        [21] -0.99
 


data frame:
``````````` 

The following commands create and display a data frame named 'daj': 
 
        > daj <- data.frame(    x=round(rnorm(15), 2), 
                                y=round(rnorm(15), 2))

 
        > daj
               x     y 
         1  0.06 -0.80
         2  1.01  0.08
         3 -1.19  1.42
         4  1.93 -0.97
         5  0.87  1.16
         6 -1.30 -0.31
         7  0.03  0.51
         8  0.69  0.15
         9 -0.32 -0.93
        10  1.39 -2.37
        11 -0.31  1.49
        12 -1.12 -1.45
        13  1.05 -0.68
        14 -1.38 -0.85
        15  0.79 -0.29


The following commands create and display a vector whose elements
are the negative-valued elements from the second column of the
data frame 'daj': 

        > test <- daj[ daj[,2] < 0, 2 ]
 
        > test
        [1] -0.80 -0.97 -0.31 -0.93 -2.37 -1.45 -0.68 -0.85 -0.29



logical test
````````````

The following command initializes the test vector:

        > test <- as.logical(rep(0, length(daj[,2])))


The following command displays the initialized test vector:
 
        > test
         [1] F F F F F F F F F F F F F F F


The following command updates the test vector to be true wherever
the second column of 'daj' is less than zero:
 
        > test[ daj[,2] < 0 ] <- T


The following command displays the resulting logical test vector:
 
        > test
         [1] T F F T F T F F T T F T T T T
 

You can find more information on the S-PLUS operators and functions
mentioned in this discussion in the on-line help files that are displayed
when you type

        ?df
        ?set.seed
        ?round
        ?rnorm
        ?"<-"
        ?"("
        ?"["
        ?"<"
        ?data.frame
        ?as.logical
        ?rep
        ?length
        ?masked
and
        ?find

at the S-PLUS prompt, in the Commands window.


I hope this helps,

-Doug Johnson
MathSoft Seattle 
************************************************************


From: Renaud Lancelot <lancelot@telecomplus.sn>

Hi Kim,

See apply():

> M <- matrix(-10:9, ncol = 5)
> M
     [,1] [,2] [,3] [,4] [,5] 
[1,]  -10   -6   -2    2    6
[2,]   -9   -5   -1    3    7
[3,]   -8   -4    0    4    8
[4,]   -7   -3    1    5    9
> apply(M, 2, FUN = function(x) any(x < 0))
[1] T T T F F

Careful with any() or all()...

Hope this helps,

Renaud
************************************************************

<Prev in Thread] Current Thread [Next in Thread>
  • [no subject], Unknown <=