Skip to content

Instantly share code, notes, and snippets.

@flare9x
Created April 13, 2018 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flare9x/e57f55c80cdfd1d9f9ae942b0dfd84f8 to your computer and use it in GitHub Desktop.
Save flare9x/e57f55c80cdfd1d9f9ae942b0dfd84f8 to your computer and use it in GitHub Desktop.
Julia data.table::rleid() function
function rleid(x::AbstractVector)
isempty(x) && return Int[]
rle = similar(x, Int)
idx = 1
rle[1] = idx
prev = x[1]
for i in 2:length(x)
this = x[i]
if ismissing(this)
if !ismissing(prev)
prev = this
idx += 1
end
else
if ismissing(prev) || this != prev
prev = this
idx += 1
end
end
rle[i] = idx
end
rle
end
# test
Main> rleid([missing,3,4,4,missing,1,1,missing,missing,6])
10-element Array{Int64,1}:
1
2
3
3
4
5
5
6
6
7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment