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 27, 2018 in Data Analytics by Tyrion anex
• 8,280 points
47 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 27, 2018 by Sophie may
• 9,870 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,690 points
90 views
0 votes
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,040 points
366 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,040 points
120 views
0 votes
2 answers

How to sort a data frame by columns in R?

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

answered Aug 21 in Data Analytics by anonymous
• 31,840 points
149 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 Anmol
• 3,620 points
76 views
0 votes
1 answer

How to know the type of an object?

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

answered Sep 19, 2018 in Data Analytics by Anmol
• 3,620 points
39 views
0 votes
1 answer

Remove NA values from the output in R programming

Edit your code: columnmean <- function(x, removeNA = ...READ MORE

answered Oct 31, 2018 in Data Analytics by Kalgi
• 42,430 points
51 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 in Data Analytics by anonymous
• 31,840 points
87 views
0 votes
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
• 9,870 points
42 views