gpt4 book ai didi

r data.table 行为, bool 值作为列选择器

转载 作者:行者123 更新时间:2023-12-04 10:30:19 27 4
gpt4 key购买 nike

我对 data.table 的行为感到有点惊讶.我想从 data.table 中的一行中进行选择所有非 NA值。

NA它的工作值(value):

t = data.table(a=1,b=NA)
t[, !is.na(t), with=F]

NA值它不起作用:
t = data.table(a=1, b=2)
t[, !is.na(t), with=F]

基本的区别在于 t[, !c(F, F), with=F]不起作用。有趣的是 t[, c(T, T), with=F]做得很好。

我知道有很多方法可以实现所需的输出,但我只对此感兴趣 - 对我来说很奇怪 - data.table 的行为.

最佳答案

我调查了 data.table:::`[.data.table` source code

对我来说它确实看起来像一个错误。基本上发生的是!is.na()电话分为!is.na()调用。然后,它把这个向量相加,如果长度为零,它返回 null.data.table() .问题是,对于 dt <- data.table(a = 1, b = 2) , sum(is.na(dt))将永远为零。

下面是一个简短的代码,用于说明引擎盖下的内容

sim_dt <- function(...) {

## data.table catches the call
jsub <- substitute(...)
cat("This is your call:", paste0(jsub, collapse = ""))

## data.table separates the `!` from the call and sets notj = TRUE instead
## and saves `is.na(t)` into `jsub`
if (is.call(jsub) && deparse(jsub[[1L]], 500L, backtick=FALSE) %in% c("!", "-")) { # TODO is deparse avoidable here?
notj = TRUE
jsub = jsub[[2L]]
} else notj = FALSE

cat("\nnotj:", notj)
cat("\nThis is the new jsub: ", paste0(jsub, collapse = "("), ")", sep = "")

## data.table evaluates just the `jsub` part which obviously return a vector of `FALSE`s (because `!` was removed)
cat("\nevaluted j:", j <- eval(jsub, setattr(as.list(seq_along(dt)), 'names', names(dt)), parent.frame()))# else j will be evaluated for the first time on next line

## data.table checks if `j` is a logical vector and looks if there are any TRUEs and gets an empty vector
if (is.logical(j)) cat("\nj after `which`:", j <- which(j))

cat("\njs length:", length(j), "\n\n")

## data.table checks if `j` is empty (and it's obviously is) and returns a null.data.table
if (!length(j)) return(data.table:::null.data.table()) else return(dt[, j, with = FALSE])

}


## Your data.table
dt <- data.table(a = 1, b = 2)
sim_dt(!is.na(dt))
# This is your call: !is.na(dt)
# notj: TRUE
# This is the new jsub: is.na(dt)
# evaluted j: FALSE FALSE
# j after `which`:
# js length: 0
#
# Null data.table (0 rows and 0 cols)


dt <- data.table(a = 1, b = NA)
sim_dt(!is.na(dt))

# This is your call: !is.na(dt)
# notj: TRUE
# This is the new jsub: is.na(dt)
# evaluted j: FALSE TRUE
# j after `which`: 2
# js length: 1
#
# b
# 1: NA

关于r data.table 行为, bool 值作为列选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50602494/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com