How to split a vector (or a dataframe) in two parts?

Hi,

I would like to cut this vector in half:

julia> y
5-element Vector{Int64}:
 325
 328
 329
 330
 331

to obtain, let’s say a and b:

julia> a
2-element Vector{Int64}:
 325
 328

julia> b
3-element Vector{Int64}:
 329
 330
 331

I digged around but surprisingly didn’t find a simple way to do that.

If you don’t need the original Vector:

julia> y = [325, 328, 329, 330, 331]
5-element Vector{Int64}:
 325
 328
 329
 330
 331

julia> a = splice!(y, 1:div(length(y),2))
2-element Vector{Int64}:
 325
 328

julia> y
3-element Vector{Int64}:
 329
 330
 331
2 Likes
julia> y=collect(1:10)
10-element Vector{Int64}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

julia> a,b = @views y[1:end÷2],y[end÷2+1:end]
([1, 2, 3, 4, 5], [6, 7, 8, 9, 10])

you can remove @views to do copies. Or maybe this is not simple enough?

3 Likes

Thanks a lot !
I went into splice but I guess I used it wrongly.
Second method is nice too !