按顺序匹配矢量

时间:2018-05-02 07:55:50

标签: r vector string-matching

我有2个向量。

x=c("a", "b", "c", "d", "a", "b", "c")
y=structure(c(1, 2, 3, 4, 5, 6, 7, 8), .Names = c("a", "e", "b", 
"c", "d", "a", "b", "c"))

我希望相应地将aabb相匹配,以便x[2]匹配y[3]而不是y[7] x[5];和y[6]匹配y[1]而非lapply(x, function(z) grep(z, names(y), fixed=T)) ,依此类推。

[[1]]
[1] 1 6

[[2]]
[1] 3 7

[[3]]
[1] 4 8

[[4]]
[1] 5

[[5]]
[1] 1 6

[[6]]
[1] 3 7

[[7]]
[1] 4 8

给出:

1 3 4 5 6 7 8

匹配所有实例。我如何得到这个序列:

x

那么y中的元素可以相应地映射到~/Documents/GitProjects中的相应值吗?

6 个答案:

答案 0 :(得分:10)

您实际上在寻找pmatch

pmatch(x,names(y))
[1] 1 3 4 5 6 7 8

答案 1 :(得分:6)

您可以根据每个元素出现的次数更改名称属性,然后更改子集y:

x2 <- paste0(x, ave(x, x, FUN=seq_along))
#[1] "a1" "b1" "c1" "d1" "a2" "b2" "c2"
names(y) <- paste0(names(y), ave(names(y), names(y), FUN=seq_along))
y[x2]
#a1 b1 c1 d1 a2 b2 c2 
# 1  3  4  5  6  7  8 

答案 2 :(得分:3)

使用Reduce

的另一个选项
Reduce(function(v, k) y[-seq_len(v)][k],
    x=x[-1L],
    init=y[x[1L]], 
    accumulate=TRUE)

答案 3 :(得分:2)

好吧,我用for-loop

做了
#Initialise the vector with length same as x.
answer <- numeric(length(x))
for (i in seq_along(x)) {
  #match the ith element of x with that of names in y.
  answer[i] <- match(x[i], names(y))
  #Replace the name of the matched element to empty string so next time you 
  #encounter it you get the next index.
  names(y)[i] <- ""
}

answer
#[1] 1 3 4 5 6 7 8

答案 4 :(得分:2)

另一种可能性:

l <- lapply(x, grep, x = names(y), fixed = TRUE)

i <- as.integer(ave(x, x, FUN = seq_along))

mapply(`[`, l, i)

给出:

[1] 1 3 4 5 6 7 8

答案 5 :(得分:2)

与Ronak类似的解决方案,但它不会持续改变y

yFoo<-names(y)
sapply(x,function(u){res<-match(u,yFoo);yFoo[res]<<-"foo";return(res)})

结果

#a b c d a b c 
#1 3 4 5 6 7 8