gpt4 book ai didi

R: is.na() 不获取 NA 值

转载 作者:行者123 更新时间:2023-12-04 11:31:16 29 4
gpt4 key购买 nike

所以我有一个数据集,只需查看它,数据集中就有明显的 NA。

 > dput(bmi.cig)
structure(list(MSI.subset.BMI = structure(c(4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 1L, 2L, 3L, 3L, 1L, 3L, 3L, 1L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("0", "1", "2",
"NA"), class = "factor"), MSI.subset.Cigarette = structure(c(3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 1L, 2L, 1L, 2L,
2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("1",
"2", "NA"), class = "factor")), .Names = c("MSI.subset.BMI",
"MSI.subset.Cigarette"), row.names = c(NA, 30L), class = "data.frame")


> head(bmi.cig)
MSI.subset.BMI MSI.subset.Cigarette
1 NA NA
2 NA NA
3 NA NA
4 NA NA
5 NA NA
6 NA NA

我想删除任何列中包含 NA 的行,因此我使用了 ForImp 包中的列表删除函数 ld。但是,R 不识别 NA 值。

is.na(bmi.cig$MSI.subset.BMI)

我明白了

    > is.na(bmi.cig$MSI.subset.BMI)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[26] FALSE FALSE FALSE FALSE FALSE

因此,一旦我使用 ld 函数,我只会得到一个空数据集作为返回。

最佳答案

b/c 列是因子,级别是 “NA”。即,尝试

data <- structure(list(MSI.subset.BMI = structure(c(4L, 4L, 4L, 4L, 4L, 
+ 4L, 4L, 4L, 4L, 4L, 4L, 1L, 2L, 3L, 3L, 1L, 3L, 3L, 1L, 4L, 4L,
+ 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("0", "1", "2",
+ "NA"), class = "factor"), MSI.subset.Cigarette = structure(c(3L,
+ 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 1L, 2L, 1L, 2L,
+ 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("1",
+ "2", "NA"), class = "factor")), .Names = c("MSI.subset.BMI",
+ "MSI.subset.Cigarette"), row.names = c(NA, 30L), class = "data.frame")
> class(blah[,1])

data[,1]=="NA"

NA 实际上是字符 (class("NA")),而不是像 class(NA) 这样的 logical 类)

关于R: is.na() 不获取 NA 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30381152/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com