indexing - R: Index to unique vector that returns original -
i have vector v <- c(6,8,5,5,8) of can obtain unique values using
> u <- unique(v) > u [1] 6 8 5 now need index i = [2,3,1,1,3] returns original vector v when indexed u.
> u[i] [1] 6,8,5,5,8 i know such index can generated automatically in matlab, the ci index, not seem part of standard repertoire in r. aware of function can this?
the background have several vectors anonymized ids long character strings:
ids "ptefkd43fmkl28en==3rnl4" "cmdrew3rfds32fdsdd;32ff" "ptefkd43fmkl28en==3rnl4" "ptefkd43fmkl28en==3rnl4" "cmdrew3rfds32fdsdd;32ff" to reduce file size , simplify code, want transform them integers of sort
ids 1 2 1 1 2 and found index of unique vector this. since there many rows, hesitant write function loops on each element of unique vector , wonder whether there more efficient way — or different way transform character strings matching integers.
try match
df1$ids <- with(df1, match(ids, unique(ids)) ) df1$ids #[1] 1 2 1 1 2 or can convert factor , coerce numeric
with(df1,as.integer(factor(ids, levels=unique(ids)))) #[1] 1 2 1 1 2 using u , v. based on output of 'u' in op's post, must have been sorted
u <- sort(unique(v)) match(v, u) #[1] 2 3 1 1 3 or using findinterval. make sure 'u' sorted.
findinterval(v,u) #[1] 2 3 1 1 3
Comments
Post a Comment