R data.table NA类型一致性 [英] R data.table NA type consistency
问题描述
dt = data.table(x = c(1,1,2,2,2,2,3,3,3,3))
dt[, y := if(.N > 2) .N else NA, by = x] # fail
dt[, y := if(.N > 2) .N else NA_integer_, by = x] # good
第一次分组失败,因为NA
具有类型并且不是整数.有没有办法告诉数据表忽略它,并尝试使所有NA变为保持一致性的任何类型?
This first grouping fails because NA
has a type and it's not integer. Is there a way to tell data table to ignore that and try to make all NAs to whatever type that keeps consistency?
我可以在此处手动设置NA_integer
,但是如果我有很多不同类型的列,则很难正确设置所有NA类型.
I can manually set NA_integer
here, but if I have lots of columns of different types, it's hard to set all NA type correct.
顺便说一句,我应该为Date/IDate/ITime使用哪种NA类型?
BTW, what NA type should I use for Date/IDate/ITime?
推荐答案
OP的第一个问题:是否有一种方法可以告诉数据表忽略该问题,并尝试使所有NA变为保持一致性的任何类型?
OP's first question: Is there a way to tell data table to ignore that and try to make all NAs to whatever type that keeps consistency?
不.如果没有分配,您会看到类似的错误:
No. You'll see a similar error without the assignment:
dt[, if(.N > 2) .N else NA, by = x]
# Error in `[.data.table`(dt, , if (.N > 2) .N else NA, by = x) :
# Column 1 of result for group 2 is type 'integer' but expecting type 'logical'. Column types must be consistent for each group.
我认为,此每个组的列类型必须一致".消息也应针对您的情况显示.
In my opinion, this "Column types must be consistent for each group." message should be shown for your case as well.
OP的第二个问题:顺便说一句,我应该为Date/IDate/ITime使用哪种NA类型?
OP's second question: BTW, what NA type should I use for Date/IDate/ITime?
对于IDate等人,我总是通过NA_integer_
进行子集化,这似乎给出了一个长度为1的NA切片,例如as.IDate(Sys.Date())[NA_integer_]
.我不知道那是应该做的,但是我不知道有一个更好的主意.插图:
For IDate et al, I always subset by NA_integer_
, which seems to give a length-one NA slice, e.g., as.IDate(Sys.Date())[NA_integer_]
. I don't know if that's what one should do, but I don't know of a better idea. An illustration:
z = IDateTime(factor(Sys.time()))
# idate itime
# 1: 2016-08-01 16:05:25
str( lapply(z, function(x) x[NA_integer_]) )
# List of 2
# $ idate: IDate[1:1], format: NA
# $ itime:Class 'ITime' int NA
这篇关于R data.table NA类型一致性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!