在 R 中是否使用引号分配 NA
Assigning NA using quotes or not in R
我有一些数据,其中缺失值未编码为 NA,我想将它们更改为 NA,以便由估算缺失数据的 R 包自动处理。我使用的代码是这样的:
levels(data$catagorical_var)[levels(data$categorical_var) == "BLANK"] <- NA
data$numeric_var[data$numeric_var == -2] <- NA
我的问题是,对于分类变量,我应该在 NA 周围加上引号吗?有关系吗?当我使用引号和不使用引号时,它会给我不同的结果,我不确定哪个是正确的。
不,您不应该使用引号。 NA
是 R 的缺失值。 "NA"
是一个包含[=36=]和"A"两个字母的字符串。
实际上 NA
有不同的类型(针对每个 R 的数据类型)。它们用下划线指定,例如 NA_integer_
或 NA_character_
。但几乎从不需要使用它们,R 会自动使用正确的。您的确切问题实际上包含在 ?NA
的 详细信息 部分(第一段):
The NA
of character type is distinct from the string "NA"
. Programmers who need to specify an explicit missing string should use NA_character_
(rather than "NA"
) or set elements to NA
using is.na<-
.
您的代码 ..character vector.. <- NA
属于“使用 is.na<-
. 将元素设置为 NA
”
我有一些数据,其中缺失值未编码为 NA,我想将它们更改为 NA,以便由估算缺失数据的 R 包自动处理。我使用的代码是这样的:
levels(data$catagorical_var)[levels(data$categorical_var) == "BLANK"] <- NA
data$numeric_var[data$numeric_var == -2] <- NA
我的问题是,对于分类变量,我应该在 NA 周围加上引号吗?有关系吗?当我使用引号和不使用引号时,它会给我不同的结果,我不确定哪个是正确的。
不,您不应该使用引号。 NA
是 R 的缺失值。 "NA"
是一个包含[=36=]和"A"两个字母的字符串。
实际上 NA
有不同的类型(针对每个 R 的数据类型)。它们用下划线指定,例如 NA_integer_
或 NA_character_
。但几乎从不需要使用它们,R 会自动使用正确的。您的确切问题实际上包含在 ?NA
的 详细信息 部分(第一段):
The
NA
of character type is distinct from the string"NA"
. Programmers who need to specify an explicit missing string should useNA_character_
(rather than"NA"
) or set elements toNA
usingis.na<-
.
您的代码 ..character vector.. <- NA
属于“使用 is.na<-
. 将元素设置为 NA
”