匹配R中两个向量的值

时间:2012-12-07 16:39:18

标签: r

我有两个向量:

A <- c(1,3,5,6,4,3,2,3,3,3,3,3,4,6,7,7,5,4,4,3) # 7 unique values
B <- c("a","b","c","d","e","f","g")   # 7 different values

我想将B的值与A匹配,以便A中的最小值从B获得第一个值并继续到最大值。

上面的例子是:

A:        1 3 5 6 4 3 2 3 3 3 3 3 4 6 7 7 5 4 4 3
assigned: a c e f d c b c c c c c d f g g e d d c

3 个答案:

答案 0 :(得分:6)

试试这个:

A <- c(1,3,5,6,4,3,2,3,3,3,3,3,4,6,7,7,5,4,4,3)
B <- letters[1:7]

B[match(A, sort(unique(A)))]
#  [1] "a" "c" "e" "f" "d" "c" "b" "c" "c" "c" "c" "c" "d" "f" "g"
# [16] "g" "e" "d" "d" "c"

答案 1 :(得分:3)

处理@JoshO'Brien地址的一般情况的另一个选项是

B[as.numeric(factor(A))]
# [1] "a" "c" "e" "f" "d" "c" "b" "c" "c" "c" "c" "c" "d"
# [14] "f" "g" "g" "e" "d" "d" "c"

A2<-ifelse(A > 4, A + 1, A)
# [1] 1 3 6 7 4 3 2 3 3 3 3 3 4 7 8 8 6 4 4 3
B[as.numeric(factor(A2))]
# [1] "a" "c" "e" "f" "d" "c" "b" "c" "c" "c" "c" "c" "d"
# [14] "f" "g" "g" "e" "d" "d" "c"

然而,以下基准测试显示此方法比@JoshOBrien慢。

library(microbenchmark)
B <- make.unique(rep(letters, length.out=1000))
A <- sample(seq_along(B), replace=TRUE)
unique_sort_match <- function() B[match(A, sort(unique(A)))]
factor_as.numeric <- function() B[as.numeric(factor(A))]
bm<-microbenchmark(unique_sort_match(), factor_as.numeric(), times=1000L)
plot(bm)

enter image description here

答案 2 :(得分:2)

详细说明@Josh的答案中的评论:

如果A确实代表了B元素的排列(即1中的A代表B的第一个元素,4中的A表示B中的第4个元素,等等,然后正如@Matthew Plourde指出的那样,您只想使用A作为索引B

B[A]

如果A 代表B的排列,那么你应该使用@Josh

建议的方法