简体   繁体   中英

R data.table: using %like% with setkey

I am using the data.table package and have a data table dt with two columns: result and moves . I want to get the count of times where result is "1-0" and moves contains "W1.e4". This can be done as follows:

nrow(dt[result == "1-0" & moves %like% "W1.e4"])

I am using the setkey function of data.table as follows:

setkey(dt,result,moves)

However, now trying to write the above command as follows:

dt[.("1-0", %like% "W1.e4")]

Results in the error:

Error: unexpected SPECIAL in "dt[.("1-0", %like%"

When using setkey in data.table how should a command be written to include %like% ?

As David points out, that's not exactly how the %like% operator works; x %like% y will return a logical vector with the same length as x (examine print(`%like%`) ).

You might try the following instead:

dt[.('1-0', grep('W1.e4', unique(moves), value = TRUE, fixed = TRUE))]

This latter will return the actual values of moves that contain W1.e4 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM