Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-如何相交()并包含重复项?_R_Character_Intersect - Fatal编程技术网

R-如何相交()并包含重复项?

R-如何相交()并包含重复项?,r,character,intersect,R,Character,Intersect,我有以下字符字段,我正试图相交。这些应该是平等的 > 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(ch

我有以下字符字段,我正试图相交。这些应该是平等的

> 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"

它删除了我相信的两个条目,因此它已经返回了一个“s”,并且不会再执行第二次操作

它删除了我相信的两个条目,因此它已经返回了一个“s”,并且不会再执行第二次操作

使用
intersect
将返回公共元素,但不会复制它们。例如,
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"

a使用
intersect
将返回公共元素,但不会复制它们。例如,
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"

a这完全取决于所比较的向量(以及顺序),但这是否足够

b <- a <- c('E', 'd', 'w', 'a', 'r', 'd', 's', '', 'L', 'i', 'f', 'e', 's', 'c', 'i', 'e', 'n', 'c', 'e')
c <- letters[sample(1:26,100, rep=T)]

a[is.element(a,b)]
#  [1] "E" "d" "w" "a" "r" "d" "s" ""  "L" "i" "f" "e" "s" "c" "i" "e" "n" "c" "e"

a[is.element(a,c)]
# [1] "d" "w" "a" "r" "d" "s" "i" "f" "e" "s" "c" "i" "e" "n" "c" "e"

b这完全取决于您正在比较的向量(以及顺序),但这是否足够

b <- a <- c('E', 'd', 'w', 'a', 'r', 'd', 's', '', 'L', 'i', 'f', 'e', 's', 'c', 'i', 'e', 'n', 'c', 'e')
c <- letters[sample(1:26,100, rep=T)]

a[is.element(a,b)]
#  [1] "E" "d" "w" "a" "r" "d" "s" ""  "L" "i" "f" "e" "s" "c" "i" "e" "n" "c" "e"

a[is.element(a,c)]
# [1] "d" "w" "a" "r" "d" "s" "i" "f" "e" "s" "c" "i" "e" "n" "c" "e"

b我遇到了完全相同的问题,没有找到解决方案,因此我创建了自己的小函数“intersectdup”:


intersectdup我遇到了完全相同的问题,没有找到解决方案,因此我创建了自己的小函数“intersectdup”:


intersectdup了解克莱门斯,以下是基于
c的结构中的一个简单函数:

intersectMe = function(x, y, duplicates=TRUE)
    {
    xyi = intersect(x,y);
    if(!duplicates) { return (xyi); }
    
    res = c();  
    for(xy in xyi)
        {
        y.xy = which(y == xy);  ny.xy = length(y.xy);
        x.xy = which(x == xy);  nx.xy = length(x.xy);
        
        min.xy = min(ny.xy, nx.xy);
        
        res = c(res, rep(xy, min.xy) );
        }
    
    res;    
    }

说到克莱门斯,这里有一个基于
c的结构中的简单函数:

intersectMe = function(x, y, duplicates=TRUE)
    {
    xyi = intersect(x,y);
    if(!duplicates) { return (xyi); }
    
    res = c();  
    for(xy in xyi)
        {
        y.xy = which(y == xy);  ny.xy = length(y.xy);
        x.xy = which(x == xy);  nx.xy = length(x.xy);
        
        min.xy = min(ny.xy, nx.xy);
        
        res = c(res, rep(xy, min.xy) );
        }
    
    res;    
    }

vecsets
库也有帮助(使用Eric创建的示例)


vecsets
库也有帮助(使用Eric创建的示例)


也许您正在寻找
setequal
<代码>相交
提供两个向量之间的公共(非重复)元素。例如,
intersect(c(“a”,“b”),c(“b”,“c”)
给出了
“b”
,但是输出结果应该是两个字符串中的公共字符。我需要的是这个答案,而不是一个正确/错误的答案,所以“不重复”是我的问题所在。有没有办法做到这一点并包括DUP?这是一个更复杂的问题,因为当向量是
c(“b”,“b”)
c(“b”)
时,你会怎么做?您希望返回的是
“b”
还是
c(“b”,“b”)
?可能您正在寻找
setequal
<代码>相交
提供两个向量之间的公共(非重复)元素。例如,
intersect(c(“a”,“b”),c(“b”,“c”)
给出了
“b”
,但是输出结果应该是两个字符串中的公共字符。我需要的是这个答案,而不是一个正确/错误的答案,所以“不重复”是我的问题所在。有没有办法做到这一点并包括DUP?这是一个更复杂的问题,因为当向量是
c(“b”,“b”)
c(“b”)
时,你会怎么做?你想要返回的是
“b”
还是
c(“b”,“b”)
vecsets::vintersect(a,b)
别以为我以前用过
vecsets
,谢谢你提醒我。返回的
“E”“d”“d”“w”“a”“r”“s”“s”“L”“i”“f”“E”“E”“c”“c”“c”“n”
,这与OP预期的不同,我认为。
vecsets::vintersect(a,b)
不要认为我以前使用过
vecsets
,谢谢你提醒我注意它。返回
“E”“d”“d”“w”“a”“r”“s”“s”“L”“i”“f”“E”“E”“E”“c”“c”“c”“n”
,这与OP所期望的不同。您能说说为什么找不到解决方案,或者为什么以前接受的答案不能满足您的需要吗?GCF 126和108。。。获取主要因素并找到它们与重复项的交叉点。你说为什么你找不到解决方案,或者为什么以前接受的答案不符合你的需要?GCF为126和108。。。获取素数因子并找到它们与重复项的交集