Skip to contents

parseRangeCode: Converts inequalities of up to two comparators stored in a vector of strings into an executable R expression. For instance, parseRangeCode(c("1 < Y < 20", "40 < Y")) will yield the expression (1<Y)&(Y<20)&(40<Y). parseRCode: simply parse an R string into an expression.

Usage

parseRangeCode(code)

Arguments

code

(Required) For parseRangeCode: A vector of strings that contain inequalities along with various seperators (see below). Each inequality should have either 1 or 2 comparators. For parseRCode: a code string. For parseRCode a character vector containing code.

Value

An expression constructed as detailed above.

Details

The parseRangeCode function converts various kinds of ranges/inequalities that are normally not handled by R into R-executable expressions. For instance it will convert inequalities of the form "A < B < C" into (A < B) & (B < C), where < may be replaced with any comparator. Moreover it allows inqualities to be concatenated by the symbols "&", ";" or "," which are all treated as equivalent to the logical "and" (i.e. "&"). Thus "A < B <C & D > E, F <= G" will be converted into (A < B)&(B < C)&(D > E)&(F <= G). In addition if code is a vector with more than one element, each element will be parsed and then concatenated into a single expression with "&". Hence c("1 < Y", "2 < Z <= 5") would become (1 < Y)&(2 < Z)&(Z <= 5).

Warning

The character "|" is not allowed in any of the expressions contained in "code" for the parseRangeCode function.

See also

Author

Romain Francois

Examples


# Examples of using subsets
exData <- data.frame( Y = rnorm(200), B = rnorm( 200 ) )
subs1 <- parseRangeCode("1 < Y < 10 & 1 > B > -2")
exData[ eval(subs1, exData), ]
#>            Y           B
#> 3   2.116061  0.34033614
#> 6   1.282502  0.98291207
#> 12  1.338200  0.12435577
#> 29  1.667191  0.19713294
#> 37  1.102575 -1.51711697
#> 55  2.037341  0.61798441
#> 58  1.095831 -1.60204937
#> 67  1.239556  0.54634346
#> 81  1.701110  0.68853900
#> 94  1.351415  0.43294918
#> 101 1.257798  0.70330255
#> 116 2.391623  0.81161381
#> 117 1.086343 -0.88924862
#> 125 2.610507 -0.68046879
#> 127 1.370383 -0.01892756
#> 128 1.594986 -0.27971014
#> 140 1.776102 -0.23840753
#> 144 1.259121  0.10030878
#> 164 1.123692  0.30706948
#> 167 1.360826 -0.16604809
#> 170 1.050666  0.73690493
#> 176 1.642866  0.30799010
#> 192 1.246571  0.60191405
#> 197 1.098519  0.31066609

subs2 <- parseRangeCode(c("1 < Y < 10", "1 > B > -2"))
exData[ eval(subs1, exData), ]
#>            Y           B
#> 3   2.116061  0.34033614
#> 6   1.282502  0.98291207
#> 12  1.338200  0.12435577
#> 29  1.667191  0.19713294
#> 37  1.102575 -1.51711697
#> 55  2.037341  0.61798441
#> 58  1.095831 -1.60204937
#> 67  1.239556  0.54634346
#> 81  1.701110  0.68853900
#> 94  1.351415  0.43294918
#> 101 1.257798  0.70330255
#> 116 2.391623  0.81161381
#> 117 1.086343 -0.88924862
#> 125 2.610507 -0.68046879
#> 127 1.370383 -0.01892756
#> 128 1.594986 -0.27971014
#> 140 1.776102 -0.23840753
#> 144 1.259121  0.10030878
#> 164 1.123692  0.30706948
#> 167 1.360826 -0.16604809
#> 170 1.050666  0.73690493
#> 176 1.642866  0.30799010
#> 192 1.246571  0.60191405
#> 197 1.098519  0.31066609

expr <- parseRCode("rnorm(30)")
eval( expr )
#>  [1] -1.32153718  0.67102674  0.35337290  1.25334482 -0.06308490 -0.90005613
#>  [7] -0.02313134  1.16028084  1.93124995 -0.41267466  1.01875516 -0.75299372
#> [13] -0.30771900  0.92931400  0.74289881  1.35006012  1.08534022  0.89700068
#> [19]  1.39243752  1.77993116 -1.15349815  1.68140422  0.88089646  0.35353023
#> [25] -0.18912469 -0.66549917  1.90262742  0.01422601 -1.97608925 -0.98116120