I have a vector in R containing repetitive elements
a<-c("A","A","A","B","B","C","A")
And I would like to know the most efficient way to transform it in a list where each element is the key, and its positions in the original vector are the values:
l<-list(A=c(1,2,3,7),B=c(4,5),C=c(6))
l
$A
[1] 1 2 3 7
$B
[1] 4 5
$C
[1] 6
split(seq_along(a), a)
# $A
# [1] 1 2 3 7
#
# $B
# [1] 4 5
#
# $C
# [1] 6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With