如何从R中的另一个向量中减去具有重复字符的完整字符向量 [英] How to subtract a complete character vector with repeated characters from the other vector in R

查看:44
本文介绍了如何从R中的另一个向量中减去具有重复字符的完整字符向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 x 中减去 y,这意味着从 x 中删除一个A"、三个B"和一个E",所以 xNew 将是 c("A", "C", "A","B","D").这也意味着

I want to subtract y from x, which means remove one "A", three "B" and one "E" from x, so xNew will be c("A", "C", "A","B","D"). It also means

length(xNew)=length(x) - length(y)
x <- c("A","A","C","A","B","B","B","B","D","E")
y <- c("A","B","B","B","E")

setdiff 不起作用,因为

setdiff doesn't work because

xNew <- setdiff(x,y)
xNew 
[1] "C" "D"

匹配也不起作用

xNew <- x[-match(y,x)]
xNew
[1] "A" "C" "A" "B" "B" "B" "D"

它把第五个位置的B"删除了3次,所以还剩下三个B".

It removes "B" on the fifth position 3 times, so there are still three "B" left.

有谁知道如何做到这一点,R 中是否有可用的函数,或者我们应该编写一个私有函数?非常感谢.

Is anyone know how to do this, is there a function available in R or we should write a private function? Thanks a lot in advance.

推荐答案

你可以使用pmatch函数:

x[-pmatch(y,x)]
#[1] "A" "C" "A" "B" "D"

编辑
如果您的数据可以是超过 1 个字符的字符串,这里有一个选项可以获取您想要的内容:

Edit
If your data can be strings of more than 1 character, here is an option to get what you want:

xNew <- unlist(sapply(x[!duplicated(x)], 
                      function(item, tab1, tab2) {
                          rep(item,
                              tab1[item] - ifelse(item %in% names(tab2), tab2[item], 0))
                       }, tab1=table(x), tab2=table(y)))

示例

x <- c("AB","BA","C","CA","B","B","B","B","D","E")
y <- c("A","B","B","B","E")
xNew
#  AB   BA    C   CA    B    D 
#"AB" "BA"  "C" "CA"  "B"  "D"

这篇关于如何从R中的另一个向量中减去具有重复字符的完整字符向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆