简体   繁体   中英

How to read the NA count in a table?

If I use useNA="always" for a table(), the resulting object will contain an entry with the name NA (not "NA" ). Is there any way to read this value?

I found a possible solution here (last row), but I guess there is a much more elegant solution?!

test = table(c(1,1,2,2,3,3,NA), useNA="always")
print(test[NA])      # <NA> <NA> <NA> <NA>
print(test[[NA]])    # subscript out of bounds
print(test["<NA>"])  # NA

test[which(is.na(names(test)))[1]]  # Works: 1, but not exactly ... elegant

Thanks

As the count of NA values is the last one, you can use tail(test, 1) .

As I understood you are looking for the extract using character indices when some name is NA . I believe it is not possible to extract directly the object with NA name. From the Extract documentation :

Neither empty ("") nor NA indices match any names, not even empty nor missing names. If any object has no names or appropriate dimnames, they are taken as all "" and so match nothing.

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