R programming Finding the difference between 2 vectors

0 votes

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
[1]  5  3 10

Instead it should be:

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]  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 28, 2018 in Data Analytics by Tyrion anex
• 8,700 points
1,344 views

1 answer to this question.

0 votes

Try this function, it worked for me:

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


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

set.seed(42)
x <- rpois(100,10)
y <- sample(x,90,replace=F)
f(x,y)
# [1] 11 12  9 10 10  9 10  4  9  6
answered Dec 28, 2018 by Sophie may
• 10,620 points

Related Questions In Data Analytics

0 votes
1 answer
0 votes
1 answer

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

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

answered Sep 5, 2018 in Data Analytics by zombie
• 3,790 points
2,652 views
+1 vote
1 answer

Difference between factor and as.factor in R programming

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

answered Oct 29, 2018 in Data Analytics by Maverick
• 10,840 points
4,866 views
0 votes
1 answer

What is the difference between R and SPSS?

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

answered Dec 11, 2018 in Data Analytics by Maverick
• 10,840 points
1,287 views
+1 vote
2 answers

How to sort a data frame by columns in R?

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

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,816 views
+10 votes
3 answers

Which is a better initiative to learn data science: Python or R?

Well it truly depends on your requirement, If ...READ MORE

answered Aug 9, 2018 in Data Analytics by Abhi
• 3,720 points
1,639 views
0 votes
1 answer

How can I perform word stemming in R

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

answered Aug 20, 2018 in Data Analytics by Abhi
• 3,720 points
4,543 views
0 votes
1 answer

How to know the type of an object?

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

answered Sep 20, 2018 in Data Analytics by Abhi
• 3,720 points
714 views
0 votes
2 answers

What is the difference between %% and % in R programming?

HI, %% returns remainder in case of numeric ...READ MORE

answered Aug 26, 2019 in Data Analytics by anonymous
• 33,030 points
2,412 views
+1 vote
1 answer

Does Geshi support the R programming language?

rplus was not included in the official ...READ MORE

answered Dec 21, 2018 in Data Analytics by Sophie may
• 10,620 points
715 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP