简体   繁体   中英

R: how to find index of all repetition vector values order by unique vector without using loop?

I have a vector of integers like this:

a <- c(2,3,4,1,2,1,3,5,6,3,2)
values<-c(1,2,3,4,5,6)

I want to list, for every unique value in my vector (the unique values being ordered), the position of their occurences. My desired output:

rep_indx<-data.frame(c(4,6),c(1,5,11),c(2,7,10),c(3),c(8),c(9))

split fits pretty well here, which returns a list of indexes for each unique value in a :

indList <- split(seq_along(a), a)
indList
# $`1`
# [1] 4 6
# 
# $`2`
# [1]  1  5 11
# 
# $`3`
# [1]  2  7 10
# 
# $`4`
# [1] 3
# 
# $`5`
# [1] 8
# 
# $`6`
# [1] 9

And you can access the index by passing the value as a character, ie:

indList[["1"]]
# [1] 4 6

You can do this, using sapply . The ordering that you need is ensured by the sort function.

sapply(sort(unique(a)), function(x) which(a %in% x))
#### [[1]]
#### [1] 4 6
#### 
#### [[2]]
#### [1]  1  5 11
#### ...

It will result in a list, giving the indices of your repetitions. It can't be a data.frame because a data.frame needs to have columns of same lengths.

sort(unique(a)) is exactly your vector variable.

NOTE: you can also use lapply to force the output to be a list. With sapply , you get a list except if by chance the number of replicates is always the same, then the output will be a matrix... so, your choice!

Perhaps this also works

order(match(a, values))
#[1]  4  6  1  5 11  2  7 10  3  8  9

您可以使用lapply函数返回带有索引的列表。

lapply(values, function (x) which(a == x))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM