Robert Brown FM CEFAS
2004-Oct-20 08:38 UTC
[R] Selecting from a character vector with logical operators
I'm trying to select a subset from character vector using the logical operator >, but get the following error 6356 85 SOL 1 25 1 38E6 6357 85 SOL 1 27 1 38E6 6910 95 SOL 1 25 1 38E6 7152 98 SOL 1 19 1 38E6 7153 98 SOL 1 22 2 38E6 7154 98 SOL 1 28 1 38E6> ca11c93SOL1VIIa<-ca11c93SOL1[ca11c93SOL1$RECTAN>"32Z9",]Warning message: ">" not meaningful for factors in: Ops.factor(ca11c93SOL1$RECTAN, "32Z9") (note RECTAN is the name assigned to the 7 th column) Do these operators act on chacters vectors as opposed to numeric vectors and if so what are the rules for this? Regards, Robert Brown
Chuck Cleland
2004-Oct-20 09:49 UTC
[R] Selecting from a character vector with logical operators
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") Robert Brown FM CEFAS wrote:> I'm trying to select a subset from character vector using the logical operator >, but get the following error > > 6356 85 SOL 1 25 1 38E6 > 6357 85 SOL 1 27 1 38E6 > 6910 95 SOL 1 25 1 38E6 > 7152 98 SOL 1 19 1 38E6 > 7153 98 SOL 1 22 2 38E6 > 7154 98 SOL 1 28 1 38E6 > >>ca11c93SOL1VIIa<-ca11c93SOL1[ca11c93SOL1$RECTAN>"32Z9",] > > Warning message: > ">" not meaningful for factors in: Ops.factor(ca11c93SOL1$RECTAN, "32Z9") > > (note RECTAN is the name assigned to the 7 th column) > > Do these operators act on chacters vectors as opposed to numeric vectors and if so what are the rules for this? > > Regards, > > Robert Brown > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 452-1424 (M, W, F) fax: (917) 438-0894
Gavin Simpson
2004-Oct-20 10:38 UTC
[R] Selecting from a character vector with logical operators
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. %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Spencer Graves
2004-Oct-20 16:07 UTC
[R] Selecting from a character vector with logical operators
One can, of course, convert the factor to character as:> letfac <- factor(letters) > letfac[as.character(letfac)>"j"][1] k l m n o p q r s t u v w x y z 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 Alternatively, one can select from the levels:> letfac[levels(letfac)[letfac]>"j"][1] k l m n o p q r s t u v w x y z 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>hope this helps. spencer graves Gavin Simpson wrote:> 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 >-- Spencer Graves, PhD, Senior Development Engineer O: (408)938-4420; mobile: (408)655-4567