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。。。获取素数因子并找到它们与重复项的交集