gpt4 book ai didi

r - 匹配 R 中两个向量的值

转载 作者:行者123 更新时间:2023-12-02 13:53:49 26 4
gpt4 key购买 nike

我有两个向量:

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

最佳答案

试试这个:

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"

关于r - 匹配 R 中两个向量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13767244/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com