Index Array without Elements

后端 未结 3 1085
温柔的废话
温柔的废话 2021-01-17 23:28

I have a vector

a = Vector(1:4)

[1, 2, 3, 4]

and I want to index it to all elements but the third to get

[1,         


        
3条回答
  •  借酒劲吻你
    2021-01-17 23:57

    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
    

    Another option is to mutate a:

    julia> deleteat!(a,3)
    3-element Array{Int64,1}:
     1
     2
     4
    

    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     │
    

    and when DataFrames is imported Not will also work with a Vector:

    julia> a[Not(3)]
    3-element Array{Int64,1}:
     1
     2
     4
    

提交回复
热议问题