Subsets a data table by specified filter(s).

datFilter(
  x,
  xfilter = NULL,
  xfiltervar = NULL,
  othertabnms = NULL,
  uniqueid = "PLT_CN",
  vardelete = NULL,
  title.filter = NULL,
  savedata = FALSE,
  filternm = NULL,
  stopifnull = FALSE,
  returnDT = TRUE,
  xnm = NULL,
  savedata_opts = NULL,
  gui = FALSE
)

Arguments

x

Data frame, sf data frame or comma-delimited file (*.csv). A data frame to filter.

xfilter

String. A filter expression. Must be R syntax. (e.g., "STATUSCD == 1", "INVYR within double quotes (e.g., "SPP == 'Lodgepole'"). If NULL, a window pops up to select filter variable(s) and filter value(s).

xfiltervar

String. The filtervar if you know what it is. If NULL, a window will pop up to select filter value(s).

othertabnms

String vector. Name(s) of the objects or comma-delimited files to subset. Names must be in quotes (e.g., othertables=c('tree', 'cond')).

uniqueid

String. Unique identifier of x. Only needed if othertables != NULL. The uniqueid must be the same for all tables except if PLT_CN and CN.

vardelete

String vector. Vector of variables you would like deleted from filter list. Mostly used for internal queries.

title.filter

String. Title of the filter query window. Mostuly used for internal queries.

savedata

Logical. If TRUE, writes output data to outfolder.

filternm

String. Optional. Name of filter, for feedback purposes.

stopifnull

Logical. If TRUE, stop if output is NULL.

returnDT

Logical. If TRUE, returns a data table. If FALSE, returns a data frame.

xnm

String. Name for filter attribute. Used for warning messages.

savedata_opts

List. See help(savedata_options()) for a list of options. Only used when savedata = TRUE. If out_layer = NULL, default = 'datf'.

gui

Logical. If TRUE, pop-up windows will appear for user-interface.

Value

A list of the following items:

xf

A data frame of filtered x.

xfilter

The xfilter.

If othertables != NULL, the other tables, named with 'in' prefix

Details

If no parameters, then user is prompted for input. If partial parameters, default parameter values are used.

Note

If message returned is 'filter removed all records', either the filter removed all records in x or the filter is incorrect.

Author

Tracey S. Frescino

Examples

# Set up data for example
tab <- data.frame(cbind(CONDCLASS=c(1, 1, 2, 1, 3, 3, 3, 1, 1, 1, 2, 1),
                         FORTYPCD = c(182, 184, 201, 221, 221, 184, 221, 182,
                                      182, 201, 182, 221)))
                          
  # Filter for value not equal to 182
  datFilter(x = tab, xfilter = "FORTYPCD != 182")
#> FORTYPCD != 182
#> filter removed 4 records: FORTYPCD != 182
#> $xf
#>    CONDCLASS FORTYPCD
#>        <num>    <num>
#> 1:         1      184
#> 2:         2      201
#> 3:         1      221
#> 4:         3      221
#> 5:         3      184
#> 6:         3      221
#> 7:         1      201
#> 8:         1      221
#> 
#> $xfilter
#> [1] "FORTYPCD != 182"
#> 

# Filter on two conditions, grab xf object from list
datFilter(x = WYcond, xfilter = "FORTYPCD == c(221) & STDSZCD == 3")$xf
#> FORTYPCD == c(221) & STDSZCD == 3
#> filter removed 3221 records: FORTYPCD == c(221) & STDSZCD == 3
#>            PLT_CN CONDID COND_NONSAMPLE_REASN_CD CONDPROP_UNADJ SUBPPROP_UNADJ
#>            <char>  <int>                   <int>          <num>          <num>
#> 1: 40405070010690      1                      NA           1.00           1.00
#> 2: 40406510010690      1                      NA           0.75           0.75
#> 3: 40406890010690      1                      NA           1.00           1.00
#>    MICRPROP_UNADJ MACRPROP_UNADJ OWNCD OWNGRPCD RESERVCD SITECLCD STDORGCD
#>             <num>         <lgcl> <int>    <int>    <int>    <int>    <int>
#> 1:           1.00             NA    22       20        0        6        0
#> 2:           0.75             NA    46       40        0        6        0
#> 3:           1.00             NA    46       40        0        6        0
#>    ADFORCD LIVE_CANOPY_CVR_PCT COND_STATUS_CD NF_COND_STATUS_CD FORTYPCD
#>      <int>               <int>          <int>             <int>    <int>
#> 1:      NA                  16              1                NA      221
#> 2:      NA                  12              1                NA      221
#> 3:      NA                  20              1                NA      221
#>    STDSZCD STDAGE GSSTKCD DSTRBCD1 DSTRBYR1 DSTRBCD2 DSTRBYR2 FORTYPGRPCD
#>      <int>  <int>   <int>    <int>    <int>    <int>    <int>       <num>
#> 1:       3     47       4        0       NA        0       NA         220
#> 2:       3     12       4        0       NA        0       NA         220
#> 3:       3     30       4        0       NA        0       NA         220
#>    TIMBERCD
#>       <num>
#> 1:        1
#> 2:        1
#> 3:        1