[R] Selecting from a character vector with logical operators

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Oct 20 12:38:47 CEST 2004


Chuck Cleland wrote:
>   With a character vector it works fine, but your ca11c93SOL1$RECTAN 
> seems to be a factor.
> 
>  > letters[letters > "j"]
>  [1] "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
> 
>  > letfact <- as.factor(letters)
>  > letfact[letfact > "j"]
>  [1] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> 
> <NA> <NA>
> [16] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
> Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z
> Warning message:
> ">" not meaningful for factors in: Ops.factor(letfact, "j")

You need an ordered factor for this to work:

 > letfact <- as.ordered(letters)
 > letfact[letfact > "j"]
  [1] k l m n o p q r s t u v w x y z
26 Levels: a < b < c < d < e < f < g < h < i < j < k < l < m < n < o < 
... < z

Gav

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson                     [T] +44 (0)20 7679 5522
ENSIS Research Fellow             [F] +44 (0)20 7679 7565
ENSIS Ltd. & ECRC                 [E] gavin.simpson at ucl.ac.uk
UCL Department of Geography       [W] http://www.ucl.ac.uk/~ucfagls/cv/
26 Bedford Way                    [W] http://www.ucl.ac.uk/~ucfagls/
London.  WC1H 0AP.
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%




More information about the R-help mailing list