How to subtract a complete character vector with repeating characters from another vector in R

I want to subtract y from x, which means deleting one โ€œAโ€, three โ€œBโ€ and one โ€œEโ€ from x, so it xNewwill 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 does not work because

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

also does not work

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

He deletes โ€œBโ€ in fifth position 3 times, so there are three more โ€œBsโ€ left.

Does anyone know how to do this, is there a function available in R, or should we write a private function? Thank you very much in advance.

+4
source share
1 answer

You can use the function pmatch:

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

Edit
1 , , :

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

All Articles