I'm creating a function in R such that if A and B are vectors. Such that B is a subset of A.

And A and B may contain repeating elements.

Then (A-B) has the remaining elements.

I started by using setdiff() but its not applicable for repeating elements.

```d<-c(1,1,1,5,5,5,3,0,10,10)
b<-c(1,1,0)
e<-setdiff(d,b)
e
  5  3 10```

`c(1,5,5,5,3,10,10)`

Since I was getting error, I created a new function such that:

```my.sample<-function(d,b){
y<-numeric()
u<-numeric()
t<-list()
x<-numeric()
rd<-rle(d)
rb<-rle(b)
h<-numeric()
d.data<-data.frame(rd\$lengths,rd\$values)
b.data<-data.frame(rb\$lengths,rb\$values)

for(i in 1:nrow(b.data)){
y[i]<-b.data[i,2]
u[i]<-b.data[i,1]
h[i]<-(d.data[d.data\$rd.values==y[i],1]-u[i])
d.data[d.data\$rd.values==y[i],1]<-h[i]
}
x<-d.data[,1]
for(j in 1:length(x))
{
t[[j]]<-rep(d.data[j,2],x[j])
}
return(unlist(t))
}```

Then I tried:

```my.sample(d,b)
  1  5  5  5  3 10 10```

This works for that specific problem, but when I tried using it to another more complicated vector like:

```x<-rpois(100,10)
y<-sample(x,25,replace=F)
my.sample(x,y)

Error in rep(d.data[j, 2], x[j]) : invalid 'times' argument
In addition: There were 21 warnings (use warnings() to see them)```

I get this error Dec 27, 2018 301 views

## 1 answer to this question.

Try this function, it worked for me:

```f <- function(d, b)
d[-unlist(tapply(b, b, function(y) head(which(d == y), length(y))))]

f(d, b)
#   1  5  5  5  3 10 10

set.seed(42)
x <- rpois(100,10)
y <- sample(x,90,replace=F)
f(x,y)
#  11 12  9 10 10  9 10  4  9  6``` answered Dec 27, 2018 by
• 10,550 points

## What is the difference between [] and [[]] notations to access the elements of a list or dataframe in R?

R provides 3 basic indexing operators. Refer ...READ MORE

## What is the difference between library () and require () functions in R ?

library() require() Library () function gives an error message ...READ MORE

+1 vote

## Difference between factor and as.factor in R programming

Hey @Ali, as.factor is a wrapper for ...READ MORE

## What is the difference between R and SPSS?

One of the main difference is R ...READ MORE

+1 vote

## How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

## How can I perform word stemming in R

The tm package in R provides the stemDocument() function to stem the ...READ MORE

## How to know the type of an object?

To find the type of an object ...READ MORE