如何获得R中两个向量之间元素的第n个匹配?

nac*_*cab 5 r

match 返回第一个和第二个参数之间的第一个匹配位置:

match(c("a","c"), c("a", "a", "b", "c", "c", "c")) # 1 4
Run Code Online (Sandbox Code Playgroud)

指定除第一个之外的匹配的最佳方法是什么?例如,我们想要第二场比赛"a"和第三场比赛"c"(所以我们得到:) 2 6.

更新:效率低下的解决方案会进行n次查找:

value_index_query <- data.frame(value = c("a", "c"), index = c(2, 3))
id <-  c("a", "a", "b", "c", "c", "c")
apply(value_index_query, 1, function(value_index) {
  value <- value_index[1]
  index <- as.integer(value_index[2])
  which(id == value)[index]
})
Run Code Online (Sandbox Code Playgroud)

42-*_*42- 7

这也使用mapply通过哪个(.)[n]操作串联运行两列.

with(value_index_query,  
     mapply( function(target, nth) which(id==target)[nth], 
               target=value, nth=index) )
[1] 2 6
Run Code Online (Sandbox Code Playgroud)


Ric*_*ven 6

这是一个data.table解决方案,我们将id向量与映射表连接起来.然后我们可以.EACHI用于分组,index.I每个组中抓取.

library(data.table)
## 'dti' would be your 'value_index_query' with the 'value' column renamed
dti <- data.table(id = c("a", "c"), index = c(2, 3))
## join it with 'id' and take 'index' by group
data.table(id)[dti, .I[index], by = .EACHI, on = "id"]$V1
# [1] 2 6
Run Code Online (Sandbox Code Playgroud)

我们可以把它放到一个函数中:

viq <- function(id, value, index) {
    dti <- data.table(id = value, index = index)
    data.table(id)[dti, .I[index], by = .EACHI, on = "id"]$V1
}

id <- c("a", "a", "b", "c", "c", "c")

viq(id, c("a", "c"), 2:3)
# [1] 2 6
viq(id, c("a", "c"), c(2, 4))
# [1]  2 NA
viq(id, c("a", "b", "c"), c(2, 1, 4))
# [1]  2  3 NA
viq(id, c("a", "b", "c"), c(2, 1, 3))
# [1] 2 3 6
Run Code Online (Sandbox Code Playgroud)