gpt4 book ai didi

r - 如何 intersect() 并包含重复项?

转载 作者:行者123 更新时间:2023-12-02 03:43:32 24 4
gpt4 key购买 nike

我有以下字符字段正在尝试相交。这些应该是相等的。

> char.y[[892]]
[1] "E" "d" "w" "a" "r" "d" "s" " " "L" "i" "f" "e" "s" "c" "i" "e" "n" "c" "e" "s"
> char.x[[892]]
[1] "E" "d" "w" "a" "r" "d" "s" " " "L" "i" "f" "e" "s" "c" "i" "e" "n" "c" "e" "s"
> intersect(char.x[[892]], char.y[[892]])
[1] "E" "d" "w" "a" "r" "s" " " "L" "i" "f" "e" "c" "n"
>

预期结果:

"E" "d" "w" "a" "r" "d" "s" " " "L" "i" "f" "e" "s" "c" "i" "e" "n" "c" "e"

最佳答案

使用intersect将返回公共(public)元素,但不会使它们重复。例如,s 出现 3 次,但仅出现一次相交。

例如,如果您想查看相同的布局,但删除了非相交值,则可以使用以下命令:

a <- c("E", "d", "w", "a", "r", "d", "s", " ", "L", "i", "f", "e", "s", "c", "i", "e", "n", "c", "e", "s")
b <- c("E", "d", "w", "a", "r", "d", "s", " ", "L", "i", "f", "e", "s", "c", "i", "e", "n", "c", "e", "s")
a[a %in% intersect(a, b)]
# [1] "E" "d" "w" "a" "r" "d" "s" " " "L" "i" "f" "e" "s" "c" "i" "e" "n" "c" "e" "s"

关于r - 如何 intersect() 并包含重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47576773/

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