没有元素的索引数组 [英] Index Array without Elements

查看:73
本文介绍了没有元素的索引数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个向量

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆