本文介绍了像 R 的 is.na 函数一样在 Julia 中查找缺失值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Julia 1.0.0 documentation 说明了 Julia 和 R 中的缺失值:

The Julia 1.0.0 documentation says this about missing values in Julia and R:

在 Julia 中,缺失值由缺失对象而不是 NA 表示.使用 ismissing(x) 代替 isna(x).skipmissing 函数通常用于代替 na.rm=TRUE(尽管在某些特定情况下,函数采用 skipmissing 参数).

这是我想在 Julia 中复制的 R 示例代码:

Here is example code in R that I would like to duplicate in Julia:

> v = c(1, 2, NA, 4)
> is.na(v)
[1] FALSE FALSE  TRUE FALSE

(首先请注意,is.na 是 R 函数的正确拼写,而不是上面引用中显示的 isna,但这不是我的意思.)

(First note that is.na is the R function's correct spelling, not isna as shown in the quote above, but that is not my point.)

如果我按照文档的建议在 Julia 中使用 ismissing,我会得到与 R 中不同类型的结果.

If I follow the documentation's suggestion to use ismissing in Julia, I get a different type of result than in R.

julia> v = [1, 2, missing, 4]
4-element Array{Union{Missing, Int64},1}:
 1
 2
  missing
 4

# Note that based on R, I was expecting: `false false true false`
# though obviously in a different output format.
julia> ismissing(v)
false

要复制 R 代码,我似乎必须执行以下操作:

To duplicate the R code, I seem to have to do something like:

julia> [ismissing(x) for x in v]
4-element Array{Bool,1}:
 false
 false
  true
 false

这行得通,但它不如 R 中的 is.na 简洁.也许我遗漏了一些东西.

That works, but it is not as succinct as is.na in R. Maybe I am missing something.

我也试过了:

julia> ismissing(v[:])
false

julia> ismissing(v[1:end])
false

有什么建议吗?

推荐答案

你可以用广播ismissing.:

You can broadcast ismissing with .:

julia> v = [1, 2, missing, 4]
4-element Array{Union{Missing, Int64},1}:
 1
 2
  missing
 4

julia> ismissing.(v)
4-element BitArray{1}:
 false
 false
  true
 false

这篇关于像 R 的 is.na 函数一样在 Julia 中查找缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 12:33