[英]data.table: Numeric column name
Here's the data.table Im working with: 这是data.table我正在使用的表:
> head(dataTable)
persnr 1993 1994
1: 60487416 0.5777598 NA
2: 60487511 NA 5.245855
3: 60488034 0.5777598 23.100167
4: 60488147 0.5777598 NA
5: 60488240 0.5777598 23.100167
6: 60488338 0.5777598 23.100167
Having the column years numeric is quite useful, as I can simply iterate through these. 具有列年份数字非常有用,因为我可以简单地遍历这些数字。 It however has a drawback:
但是它有一个缺点:
dataTable[is.na(1993),]
Empty data.table (0 rows) of 3 cols: persnr,1993,1994
It mistakes the 1993
for an integer, instead of using it as the object name. 它将
1993
误认为是整数,而不是将其用作对象名称。 Otherwise I can't explain how it would come up with zero rows that satisfy this condition. 否则,我无法解释它将如何得出满足该条件的零行。 How can I check for
NA
values when the column name is numeric? 列名称为数字时,如何检查
NA
值?
You may want to treat your data.frame like a matrix and use the apply function to find the NA values: 您可能希望将data.frame视为矩阵,然后使用apply函数查找NA值:
apply(dataTable,1,is.na)
套用(dataTable,1,is.na)
That will be faster than iterating through columns. 这将比遍历列更快。
If you want to find the rows with any NA values you could do: 如果要查找具有任何NA值的行,可以执行以下操作:
apply(dataTable,1,function(x){any(is.na(x))})
apply(dataTable,1,function(x){any(is.na(x))})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.