R tricks

This is a place where I will place some of the R tricks and script snippets that I need on a semi regular basis… but that I keep forgetting how to do.

Filtering character vectors

A common example is a need to load a list of rasters from a list of files. The challenge is that the file list can often come with unwanted items in the list (e.g. xml meta-data files).

rs <- list.files(path = "e:/tmpGIS/PEM_cvs/", pattern = "tif") 
print(rs)
##  [1] "aspect_025.tif"               "Aspect_05m.tif"              
##  [3] "Aspect_10m.tif"               "Aspect_25m.tif"              
##  [5] "Convergence_025.tif"          "Convergence_05m.tif"         
##  [7] "Convergence_10m.tif"          "Convergence_25m.tif"         
##  [9] "cov_10m.tif"                  "cov_25m.tif"                 
## [11] "dAH_05m.tif"                  "dAH_10m.tif"                 
## [13] "dAH_25m.tif"                  "Diurnal_a_heating_025.tif"   
## [15] "dtm_05m.tif"                  "dtm_10m.tif"                 
## [17] "dtm_250cm_025.tif"            "dtm_25m_25m.tif"             
## [19] "Filled_sinks_025.tif"         "Filled_sinks_05m.tif"        
## [21] "Filled_sinks_10m.tif"         "Filled_sinks_25m.tif"        
## [23] "gCurvature_05m.tif"           "gCurvature_10m.tif"          
## [25] "gCurvature_25m.tif"           "gcurve_025.tif"              
## [27] "MRRTF_025.tif"                "MRRTF_05m.tif"               
## [29] "MRRTF_10m.tif"                "MRRTF_25m.tif"               
## [31] "MRVBF_025.tif"                "MRVBF_05m.tif"               
## [33] "MRVBF_10m.tif"                "MRVBF_25m.tif"               
## [35] "N_openness_025.tif"           "OpennessNegative_05m.tif"    
## [37] "OpennessNegative_10m.tif"     "OpennessNegative_25m.tif"    
## [39] "OpennessPositive_05m.tif"     "OpennessPositive_10m.tif"    
## [41] "OpennessPositive_25m.tif"     "overland_flow_025.tif"       
## [43] "OverlandFlowDistance_05m.tif" "OverlandFlowDistance_10m.tif"
## [45] "OverlandFlowDistance_25m.tif" "P_openness_025.tif"          
## [47] "p50_10m.tif"                  "p50_25m.tif"                 
## [49] "p75_10m.tif"                  "p75_25m.tif"                 
## [51] "p90_10m.tif"                  "p90_25m.tif"                 
## [53] "p95_10m.tif"                  "p95_25m.tif"                 
## [55] "slope_025.tif"                "Slope_05m.tif"               
## [57] "Slope_10m.tif"                "Slope_10m.tif.aux.xml"       
## [59] "Slope_25m.tif"                "std_10m.tif"                 
## [61] "std_25m.tif"                  "tcurve_025.tif"              
## [63] "tCurve_05m.tif"               "tCurve_10m.tif"              
## [65] "tCurve_25m.tif"               "TPI_025.tif"                 
## [67] "TPI_05m_05m.tif"              "TPI_10m.tif"                 
## [69] "TPI_25m.tif"                  "TRI_025.tif"                 
## [71] "TRI_05m.tif"                  "TRI_10m.tif"                 
## [73] "TRI_25m.tif"                  "TWI_025.tif"                 
## [75] "TWI_05m.tif"                  "TWI_10m.tif"                 
## [77] "TWI_25m.tif"                  "Vertical_distance_025.tif"   
## [79] "VerticalDistance_05m.tif"     "VerticalDistance_10m.tif"    
## [81] "VerticalDistance_25m.tif"

Did you notice the one entry that was .tif.xml?

It actually is quite simple to remove the unwanted pattern:

rs_clean <- rs[-c(grep(".xml", rs))]
setdiff(rs, rs_clean)
## [1] "Slope_10m.tif.aux.xml"

A new operator %notin%

Another common way to filter/ subset data is to use the %in% operator. But the inverse of %in% is not immediately obvious. Here a new operator is created

The %notin% operator does not exist but it is easily created1:

`%notin%` <- Negate(`%in%`)
Avatar
Colin Chisholm

Professional Forester bridging the gap between the academic and operational worlds.

Related