Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get index of first occurence of group in a column?

Tags:

r

 C1 C2
------
a   11
a   2
a   2
b   2
b   34
c   2
c   4
c   1
d   4

how can i get index of a groupname first occurence

for example: in column A first occurence of 'b' index is 4 like that i need to get all indexes of first occurence of group

like image 731
K K Avatar asked Dec 14 '22 07:12

K K


1 Answers

With data.table package, you can get it with .I:

as.data.table(dtt)[, .(index = .I[1]), by = .(C1)]
#    C1 index
# 1:  a     1
# 2:  b     4
# 3:  c     6
# 4:  d     9

If only indices are need:

which(!duplicated(dtt$C1))
[1] 1 4 6 9
like image 52
mt1022 Avatar answered Dec 21 '22 22:12

mt1022