[英]R data.table behavior while filtering rows
I am creating a data.table in R and setting a column to be used as key. 我在R中创建一个data.table并设置一个用作键的列。 When I try to retrieve values from the data table;
当我尝试从数据表中检索值时; for the rows where there is no match I get NA values back.
对于没有匹配的行,我得到NA值。 I typically dont want that behavior in my search.
我通常不希望在我的搜索中出现这种行为。 Example below
以下示例
library(data.table)
dt <- data.table('foo'=seq(10),bar=sample(letters,10))
setkey(dt,bar)
dt[sample(letters,5)]
> dt[sample(letters,5)]
b foo
1: x 4
2: q 2
3: u 8
4: s NA
5: b NA
To remove the NA
rows simply set nomatch=0
: 要删除
NA
行,只需设置nomatch=0
:
Here is an example (I removed the random sampling so everyone can have the same results) 这是一个例子(我删除了随机抽样,所以每个人都可以得到相同的结果)
library(data.table)
dt = data.table(foo = 1:10, bar = letters[1:10])
setkey(dt, bar)
needed_letters = letters[c(1:8,11,12)] #1 - 8 are available, 11 and 12 are not
dt[J(needed_letters),nomatch=0]
Addition from Matt 来自马特的补充
Also, if you prefer nomatch=0
to be the default, you can change the default : 此外,如果您更喜欢
nomatch=0
作为默认值,则可以更改默认值:
options(datatable.nomatch=0)
dt[J(needed_letters)] # now, no NAs will be returned
You can check all arguments like this : 你可以检查这样的所有参数:
> args(data.table:::`[.data.table`)
function (x, i, j, by, keyby,
with = TRUE,
nomatch = getOption("datatable.nomatch"),
mult = "all",
roll = FALSE,
rollends = if (roll=="nearest") c(TRUE,TRUE)
else if (roll>=0) c(FALSE, TRUE)
else c(TRUE,FALSE),
which = FALSE,
.SDcols,
verbose = getOption("datatable.verbose"),
allow.cartesian = getOption("datatable.allow.cartesian"),
drop = NULL)
The arguments whose default is via getOption
can therefore have their default changed. 因此默认为via
getOption
的参数可以更改其默认值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.