简体   繁体   中英

data.table: Numeric column name

Here's the data.table Im working with:

> 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. 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?

You may want to treat your data.frame like a matrix and use the apply function to find the NA values:

apply(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:

apply(dataTable,1,function(x){any(is.na(x))})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM