Corresponding values ​​from two vectors in R

I have two vectors:

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

I would like to combine the values ​​of B with A so that the smallest value in gets the first value from B and continues to the largest.

The above example:

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
source share
3 answers

Try the following:

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"
+6
source

Another option that handles the general case is that @ JoshO'Brien addresses will be

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"

However, the following benchmark shows that this method is slower than @ JoshOBrien's.

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

+3
source

@Josh:

If it Areally is a permutation of the elements B(i.e. where a 1in Arepresents the first element B, a 4in Arepresents the 4th element in B, etc.), then, as @Matthew Plourde points out, you would just like to use Aas your index for B:

B[A]

If A is not a permutation of B, then you should use the method suggested by @Josh

+2
source

Source: https://habr.com/ru/post/1668143/


All Articles