2015-07-01 57 views
3

我想刪除一個數據幀與NA元素的行通過執行以下操作:R:爲什麼我不能在數據框中刪除NA元素的行?

cleaned_data <- data[complete.cases(data),] 

不過,我仍然沒有得到任何行相同的數據幀被刪除。我正在運行OS X 10.10.3的3.2.1 R版本。下面是數據:

> dput(data) 
structure(list(`1` = structure(c(1L, 1L, 6L, 3L, 3L), .Label = c("1", 
"2", "3", "4", "5", "NA"), class = "factor"), `2` = structure(c(5L, 
5L, 7L, 2L, 2L), .Label = c("1", "2", "3", "4", "5", "6", "NA" 
), class = "factor"), `3` = structure(c(34L, 46L, 66L, 51L, 28L 
), .Label = c("0", "1", "10", "100", "105", "11", "110", "112", 
"12", "120", "14", "15", "16", "168", "18", "2", "20", "200", 
"21", "22", "24", "25", "26", "27", "28", "29", "3", "30", "31", 
"32", "35", "36", "4", "40", "41", "42", "42099", "42131", "42134", 
"42197", "42292", "45", "48", "49", "5", "50", "54", "55", "56", 
"6", "60", "64", "65", "7", "70", "72", "75", "77", "8", "80", 
"82", "84", "85", "9", "90", "NA"), class = "factor"), `4` = structure(c(1L, 
2L, 1L, 2L, 1L), .Label = c("0", "1", "NA"), class = "factor"), 
    `5` = structure(c(1L, 1L, 1L, 1L, 1L), .Label = c("0", "1", 
    "NA"), class = "factor"), `6` = structure(c(1L, 2L, 1L, 1L, 
    1L), .Label = c("0", "1", "NA"), class = "factor"), `7` = structure(c(2L, 
    2L, 1L, 1L, 1L), .Label = c("0", "1", "NA"), class = "factor"), 
    `8` = structure(c(1L, 1L, 1L, 1L, 1L), .Label = c("0", "1", 
    "NA"), class = "factor"), `9` = structure(c(1L, 1L, 1L, 1L, 
    2L), .Label = c("0", "1", "NA"), class = "factor")), .Names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9"), row.names = c(NA, 5L 
), class = "data.frame") 
+1

我認爲那些不是'NA's。它們是恰好具有「NA」作爲其內容的字符串。 'complete.cases(data)'全部是'TRUE'。 –

+0

true,'sum(is.na(data))'給出0.如果用「真」NA替換字符NA,代碼將起作用 – mts

+0

我強烈建議退後一步並檢查數據讀取過程,因爲它看起來像'read。 xxx'沒有'na.strings =「NA」'。 – Marek

回答

3

這些都不是真正的NA S,它們是碰巧是"NA"字符串或因素。你可以把它們變成真正的NA:

data[data=="NA"] <- NA 
相關問題