@@ -257,40 +257,39 @@ This type of operation occurs quite frequently, especially while grouping (as we
257257When querying a ` data.table ` for elements that do not exist, the behavior differs based on the method used.
258258
259259``` r
260- dt <- data.table(x = letters [1 : 3 ], y = LETTERS [1 : 3 ])
261- setkeyv(dt , " x" )
260+ setkeyv(flights , " origin" )
262261```
263262
264263* ** Key-based subsetting: ` dt["d"] ` **
265264
266265 This performs a right join on the key column ` x ` , resulting in a row with ` d ` and ` NA ` for columns not found.
267266
268- ``` r
269- dt [ " d " ]
270- # Returns:
271- # x y
272- # 1: d <NA>
273- ```
267+ ``` r
268+ flights [ " XYZ " ]
269+ # Returns:
270+ # origin year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier flight tailnum ...
271+ # 1: XYZ NA NA NA NA NA NA NA NA NA NA NA NA ...
272+ ```
274273
275274* ** Logical subsetting: ` dt[x == "d"] ` **
276275
277276 This performs a standard subset operation that does not find any matching rows and thus returns an empty ` data.table ` .
278277
279- ``` r
280- dt [ x == " d " ]
281- # Returns:
282- # Empty data.table (0 rows and 2 cols): x,y
283- ```
278+ ``` r
279+ flights [ origin == " XYZ " ]
280+ # Returns:
281+ # Empty data.table (0 rows and 19 cols): year,month,day,dep_time,sched_dep_time,dep_delay,arr_time,sched_arr_time,arr_delay,...
282+ ```
284283
285284* ** Exact match using ` nomatch=NULL ` **
286285
287286 For exact matches without ` NA ` for non-existing elements, use ` nomatch=NULL ` :
288287
289- ``` r
290- dt [ " d " , nomatch = NULL ]
291- # Returns:
292- # Empty data.table (0 rows and 2 cols): x,y
293- ```
288+ ``` r
289+ flights_dt [ " XYZ " , nomatch = NULL ]
290+ # Returns:
291+ # Empty data.table (0 rows and 19 cols): year,month,day,dep_time,sched_dep_time,dep_delay,arr_time,sched_arr_time,arr_delay,...
292+ ```
294293
295294Understanding these behaviors can help prevent confusion when dealing with non-existing elements in your data.
296295
0 commit comments