2017-09-06 64 views
0

我在R中有一個包含一列因子的數據幀。該列完全空白,但不包含任何NA值。當我嘗試訪問某個列的元素時,它確實顯示了一些因子級別。R:跟蹤因子向量/數據幀中的缺失值

我想識別這種類型的缺失數據並將其刪除。我怎麼做?

這是我的數據框的副本。第二列說明我的問題:

> dput(effective_data) 
structure(list(`Not this column` = structure(c(2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L), .Label = c("", "unavailable"), class = "factor"), `This column` = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("", "EVENT_VALUE_INTRA_ENB (1)", 
"EVENT_VALUE_X2 (2)"), class = "factor")), .Names = c("Not this column", 
"This column"), class = "data.frame", row.names = c(NA, -134L 
)) 
+2

我已經將您的示例數據集添加到問題。當你想與SO上的其他人共享可複製格式的數據時,使用命令'dput(<數據集對象的名稱>)'。任何人都可以採取這樣的結果,並得到完全相同的數據,給你的問題。您可以在這裏閱讀有關最佳實踐的更多信息:https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example –

回答

0

您可以嘗試

effective_date[, !apply(effective_date,2,function(x) any(unique(x)==""))] 

解釋

通過列迭代,apply(..,2,..),尋找any列,其中unique(x) == ""

apply(effective_date,2,function(x) any(unique(x)=="")) 
# Not this column  This column 
#   FALSE   TRUE 

然後子集數據框與邏輯

effective_date[ , <logical>]