0

Given are the two data frames A and B, where A is longer than B. The values ​​in the lines of A must be present in lines of B. And if a line of A does not exist in B, it should be deleted. At the end A should have the same lines like B. Do I Need a for-loop? Thanks for your help, I hope I illustrated the problem well.

for example:

x=c(1,3,7,1,1,4,3)
y=c(2,5,5,6,2,6,4)
A<-cbind.data.frame(x,y)

x2<-c(1,3,5,1,3)
y2<-c(2,4,7,6,8)
B<-cbind.data.frame(x2,y2)



##A should like this at the end
```
x<-c(1,1,1,3)
y<-c(2,6,2,4)
A<-c(x,y)
```
review
  • 43
  • 5

1 Answers1

1

Something I have used in the past:

i = interaction(A,sep=":")%in%interaction(B,sep=":")
i
TRUE FALSE FALSE  TRUE  TRUE FALSE  TRUE

A[i,]
  x y
1 1 2
4 1 6
5 1 2
7 3 4

What is done here is join columns with a symbol you are sure not to encounter in your data (:) and then compare these two vectors. It has saved me lots of time.

boski
  • 2,437
  • 1
  • 14
  • 30
  • thank you! works this for decimal values too? – review Apr 24 '19 at 10:43
  • yep it should also work – boski Apr 24 '19 at 10:53
  • @review read about [merging with decimals](https://stackoverflow.com/q/40183163/680068), you might want to round the numbers before merging. Also relevant post https://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal – zx8754 Apr 24 '19 at 11:14