没有元素的索引数组 [英] Index Array without Elements
本文介绍了没有元素的索引数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个向量
a = Vector(1:4)
[1, 2, 3, 4]
并且我想将其索引到所有元素,但是要获取的第三个元素
and I want to index it to all elements but the third to get
[1, 2, 4]
R
中,您可以执行a[-3]
.您在Julia
中做什么?
in R
you could do a[-3]
. What do you do in Julia
?
(最终,我想遍历所有元素并将每个元素与其余元素进行比较-也许这是相关的.)
(Eventually I want to loop through all the elements and compare each of them to the rest - maybe this is relevant.)
推荐答案
不幸的是,Julia语法比R语言更冗长:
The Julia syntax will be unfortunately more verbose than those of R:
julia> a[1:end .!== 3]
3-element Array{Int64,1}:
1
2
4
另一种选择是对a进行突变:
Another option is to mutate a:
julia> deleteat!(a,3)
3-element Array{Int64,1}:
1
2
4
如果您的数据位于DataFrame
之内,则可以获得更好的语法:
If your data is within a DataFrame
than you can get a nicer syntax:
julia> df = DataFrame(a=1:4);
julia> df[Not(3),:]
3×1 DataFrame
│ Row │ a │
│ │ Int64 │
├─────┼───────┤
│ 1 │ 1 │
│ 2 │ 2 │
│ 3 │ 4 │
,并且当导入DataFrames
时,Not
也可以与Vector
一起使用:
and when DataFrames
is imported Not
will also work with a Vector
:
julia> a[Not(3)]
3-element Array{Int64,1}:
1
2
4
这篇关于没有元素的索引数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文