I have a column in a datatable with unique values that are non-sequential, say [1,2,3,100,101,200]
but what I would like is [0,1,2,3,4,5]
(or [1,2,3,4,5,6]
for R with its 1 first index.) In python, I could do something simple like:
lookup = {elem:idx for idx,elem in enumerate(column)}
reverse_lookup = {idx:elem for idx,elem in enumerate(column)}
idx_array = [lookup[elem] for elem in column]
Is there a simple way to do this in R?
Edit: Thanks to comment/answer, I've used the seq_along
function:
c_idx <- seq_along(unique(off$callgroup))
c_val <- unique(off$callgroup)
a_idx <- seq_along(unique(off$agent_idx))
a_val <- unique(off$agent_idx)
As you can see, I have four arrays. c_idx is an array of unique values, and c_val is their sequential indices (same design for agent_idx via a_idx & a_val). In my datatable there are multiple instances of any given c_val and/or a_val.
Is there a way to swap the value out? (R sees a given value for c_val, retrieves the index, retrieves the value in c_idx under the same index, and swaps them on the datatable?)
Apparently, this does not produce the desired effect:
off2$callgroup <- replace(off$callgroup,c_val,c_idx)
off2$agent_idx <- replace(off$agent_idx,a_val,a_idx)
Namely, because, the second element should be a list of true indices. Whereas these aren't indices, rather they're values.
As per the commented link, this can be achieved with seq_along
eg seq_along(4:10)
:)
https://renenyffenegger.ch/notes/development/languages/R/functions/seq_along