我有一个 data.table
有两列:一列 ID
专栏和一个 value
柱。我想把桌子分开 ID
列并运行一个函数 foo
在...上 value
柱。这个工作正常 foo
没有返回NAs。在这种情况下,我收到一个错误,告诉我组的类型不一致。我的假设是 - 从那以后 is.logical(NA)
等于 TRUE
和 is.numeric(NA)
等于 FALSE
, data.table
内部假设我想将逻辑值与数字值组合并返回错误。但是,我发现这种行为很特殊。对此有何评论?我是否会错过这里显而易见的事情或者确实是预期的行为?如果是这样,简短的解释会很棒。 (请注意,我确实知道一个解决办法:只是让 foo2
返回一个完全不可能的数字并在以后过滤。但是,这看起来很糟糕)。
这是一个例子:
library(data.table)
foo1 <- function(x) {if (mean(x) < 5) {return(1)} else {return(2)}}
foo2 <- function(x) {if (mean(x) < 5) {return(1)} else {return(NA)}}
DT <- data.table(ID=rep(c("A", "B"), each=5), value=1:10)
DT[, foo1(value), by=ID] #Works perfectly
ID V1
[1,] A 1
[2,] B 2
DT[, foo2(value), by=ID] #Throws error
Error in `[.data.table`(DT, , foo2(value), by = ID) :
columns of j don't evaluate to consistent types for each group: result for group 2 has column 1 type 'logical' but expecting type 'numeric'
你可以通过指定你的函数应该返回一个来解决这个问题 NA_real_
而不是一个 NA
默认类型。
foo2 <- function(x) {if (mean(x) < 5) {return(1)} else {return(NA)}}
DT[, foo2(value), by=ID] #Throws error
# Error in `[.data.table`(DT, , foo2(value), by = ID) :
# columns of j don't evaluate to consistent types for each group:
# result for group 2 has column 1 type 'logical' but expecting type 'numeric'
foo3 <- function(x) {if (mean(x) < 5) {return(1)} else {return(NA_real_)}}
DT[, foo3(value), by=ID] #Works
# ID V1
# [1,] A 1
# [2,] B NA
顺便提一下那个消息 foo2()
在失败时提供的信息非常丰富。它基本上告诉你你的NA是错误的类型。要解决这个问题,你只需要寻找 NA
正确类型(或类)的常量:
NAs <- list(NA, NA_integer_, NA_real_, NA_character_, NA_complex_)
data.frame(contantName = sapply(NAs, deparse),
class = sapply(NAs, class),
type = sapply(NAs, typeof))
# contantName class type
# 1 NA logical logical
# 2 NA_integer_ integer integer
# 3 NA_real_ numeric double
# 4 NA_character_ character character
# 5 NA_complex_ complex complex
你可以通过指定你的函数应该返回一个来解决这个问题 NA_real_
而不是一个 NA
默认类型。
foo2 <- function(x) {if (mean(x) < 5) {return(1)} else {return(NA)}}
DT[, foo2(value), by=ID] #Throws error
# Error in `[.data.table`(DT, , foo2(value), by = ID) :
# columns of j don't evaluate to consistent types for each group:
# result for group 2 has column 1 type 'logical' but expecting type 'numeric'
foo3 <- function(x) {if (mean(x) < 5) {return(1)} else {return(NA_real_)}}
DT[, foo3(value), by=ID] #Works
# ID V1
# [1,] A 1
# [2,] B NA
顺便提一下那个消息 foo2()
在失败时提供的信息非常丰富。它基本上告诉你你的NA是错误的类型。要解决这个问题,你只需要寻找 NA
正确类型(或类)的常量:
NAs <- list(NA, NA_integer_, NA_real_, NA_character_, NA_complex_)
data.frame(contantName = sapply(NAs, deparse),
class = sapply(NAs, class),
type = sapply(NAs, typeof))
# contantName class type
# 1 NA logical logical
# 2 NA_integer_ integer integer
# 3 NA_real_ numeric double
# 4 NA_character_ character character
# 5 NA_complex_ complex complex