为什么如果(Julia)不能将基因矢量的一个要素测试为条件评估?

发布于 2025-01-29 17:17:43 字数 1478 浏览 3 评论 0原文

我想创建一个函数,首先,它过滤了朱莉娅(Julia)中数据框的一个元素。其次,它测试元素是否“丢失”。如果答案是rue,它将返回值“ 0.0”。我的问题是控制评估“如果”不起作用,我不知道为什么。但是,如果元素是“字符串”,则控制评估有效,但是该元素是1元素向量{substring {string}}}:efftering;因此,控制评估不起作用。我想知道为什么,并且可以将矢量元素变成字符串对象。 注意:“ is equal”,'==','==='也无法工作。

例如:

example_ped = DataFrame(animal = collect(1:1:11),
                    sire = [fill(0,5); fill(4,3); fill(5,3)],
                    dam = [fill(0,4); fill(2,4); fill(3,3)])
CSV.write("ped_example.txt",example_ped, header=true,delim='\t')
pedi = CSV.read("ped_example.txt",delim = '\t', header=true, missingstrings=["0"], DataFrame)
pedi[!,1]=strip.(string.(pedi[!,1]))
pedi[!,2]=strip.(string.(pedi[!,2]))
pedi[!,3]=strip.(string.(pedi[!,3]))

函数的一部分

function computAddRel!(ped,animal_1,animal_2)
elder,recent = animal_1 < animal_2 ? (animal_1,animal_2) : (animal_2,animal_1)
sireOfrecent = ped.sire[ped.animal.==recent]
damOfrecent  = ped[ped.animal.==recent,"dam"]
if elder==recent
    f_inbreed = (sireOfrecent=="missing" || damOfrecent=="missing") ? 0.0 : 0.5*computAddRel!(ped,sireOfrecent,damOfrecent)
    adiv = 1.0 + f_inbreed
    return adiv
end
end

如果动物_1和Animal_2等于5

julia>     sireOfrecent = pedi.sire[pedi.animal.==recent]
1-element Vector{Union{Missing, Int64}}:
 missing

,则控制评估是错误的,

julia> sireOfrecent=="missing"
false
julia> isequal(sireOfrecent,"missing")
false

感谢您的时间。

I want to create a function in which, first, it filters one element of a dataframe in Julia. Second, it tests if the element is "missing". If the answer is rue, it return the value "0.0". My issue is that the control evaluation "if" does not work and I don t know why. If the element is "String" the control evaluation works, however, the element is a 1-element Vector{SubString{String}}: after filtering; thus, the control evaluation does not work. I would like to know why and it is possible to turn the vector element into a string object.
Note: "isequal", '==', '===' do not work either.

For example:

example_ped = DataFrame(animal = collect(1:1:11),
                    sire = [fill(0,5); fill(4,3); fill(5,3)],
                    dam = [fill(0,4); fill(2,4); fill(3,3)])
CSV.write("ped_example.txt",example_ped, header=true,delim='\t')
pedi = CSV.read("ped_example.txt",delim = '\t', header=true, missingstrings=["0"], DataFrame)
pedi[!,1]=strip.(string.(pedi[!,1]))
pedi[!,2]=strip.(string.(pedi[!,2]))
pedi[!,3]=strip.(string.(pedi[!,3]))

Part of the function

function computAddRel!(ped,animal_1,animal_2)
elder,recent = animal_1 < animal_2 ? (animal_1,animal_2) : (animal_2,animal_1)
sireOfrecent = ped.sire[ped.animal.==recent]
damOfrecent  = ped[ped.animal.==recent,"dam"]
if elder==recent
    f_inbreed = (sireOfrecent=="missing" || damOfrecent=="missing") ? 0.0 : 0.5*computAddRel!(ped,sireOfrecent,damOfrecent)
    adiv = 1.0 + f_inbreed
    return adiv
end
end

if the animal_1 and animal_2 are equal to 5

julia>     sireOfrecent = pedi.sire[pedi.animal.==recent]
1-element Vector{Union{Missing, Int64}}:
 missing

However, the control evaluation is false

julia> sireOfrecent=="missing"
false
julia> isequal(sireOfrecent,"missing")
false

Thank in advance for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不爱素颜 2025-02-05 17:17:43

您应该写:

ismissing(only(sireOfrecent))

此的含义:

  • 检查是否完全选择了一行(如果不是 - 您会遇到错误,则为歧义;如果是 - 如果是 - 您从一个中提取了元素array)
  • iSmissing是您应该使用的函数,以检查某些值是否缺失

这里有一些例子:

julia> x = [missing]
1-element Vector{Missing}:
 missing

julia> only(x)
missing

julia> ismissing(only(x))
true

julia> only([1, 2])
ERROR: ArgumentError: Collection has multiple elements, must contain exactly 1 element

julia> ismissing(only([1]))
false

You should write:

ismissing(only(sireOfrecent))

The meaning of this:

  • only checks if you picked exactly one row (if not - you will get an error, as then there is ambiguity; if yes - you extract out the element from an array)
  • ismissing is a function that you should use to check if some value is missing.

Here are some examples:

julia> x = [missing]
1-element Vector{Missing}:
 missing

julia> only(x)
missing

julia> ismissing(only(x))
true

julia> only([1, 2])
ERROR: ArgumentError: Collection has multiple elements, must contain exactly 1 element

julia> ismissing(only([1]))
false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文