gpt4 book ai didi

r - 合并 2 个向量并删除所有重复项

转载 作者:行者123 更新时间:2023-12-03 16:42:01 24 4
gpt4 key购买 nike

假设有一个向量:

v1 <- c("ab", "bc", "cd", "ef", "yolo", "da", "sm", "ez-de") 
v2 <- c("ab", "bc", "cd", "ef", "yolo-da", "sm", "ez", "de")

你如何合并上面的两个向量,以便我们得到以下结果?
c("ab", "bc", "cd", "ef", "yolo-da", "sm", "ez-de") 

请注意,上面的两个向量具有相同的长度..

最佳答案

如果值的顺序不是问题,我们可以试试这个:

v <- union(v1, v2)
Filter(function(x) length(grep(x, v))==1, v)
# [1] "ab" "bc" "cd" "ef" "sm" "ez-de" "yolo-da"

关于r - 合并 2 个向量并删除所有重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42017509/

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