R:为什么我不能删除数据框中包含NA元素的行?

时间:2021-10-03 19:19:37

I am trying to delete the rows with NA elements in a data frame by doing the following:

我正在尝试删除数据框中包含NA元素的行,方法如下:

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

However, I am still getting the same data frame without any row being removed. I am running the 3.2.1 R version for OS X 10.10.3. Here is the 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 个解决方案

#1


3  

Those aren't true NAs, they are strings or factors that happen to be "NA". You can turn them into real NAs:

这些不是真正的NAs,它们是字符串或因子碰巧是“NA”。你可以把它们变成真正的NAs:

data[data=="NA"] <- NA

#1


3  

Those aren't true NAs, they are strings or factors that happen to be "NA". You can turn them into real NAs:

这些不是真正的NAs,它们是字符串或因子碰巧是“NA”。你可以把它们变成真正的NAs:

data[data=="NA"] <- NA