Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtracting a list of names from a bigger list in R

Tags:

r

I have 2 datasets.

A = 3085 rows, 1 column. B = 527 rows, 1000 columns.

All values in both of these datasets are the names of shapefiles.

I would like to create a new list of A - B[,1]. A.k.a I would like to remove any values from A that appear in the first column of B.

I will eventually be looping this for all 1000 columns.

If anyone can help, it would be much appreciated.

Regards,

like image 265
JPD Avatar asked Dec 13 '25 15:12

JPD


2 Answers

If A and B are data.frames or matrices you can use such procedure

A[!(A[,1] %in% B[,1]), 1]

I just now realized fully your question. To loop on all columns of B you can use apply family function. This call will iterate each column of B as x parameter and will return list of length equal to the number of columns of B, each element of the list will be a vector of mismatched elements of A to corresponding column of B.

apply(B, 2, function(x) A[!(A[,1] %in% x), 1])
like image 82
DrDom Avatar answered Dec 15 '25 04:12

DrDom


Something simple (but untested):

x <- A[, 1]
keep <- seq_along(x)
for(i in seq_along(B))
    keep <- setdiff(keep, which(x[keep] %in% B[, i]))
A[keep, ]
like image 37
Hong Ooi Avatar answered Dec 15 '25 04:12

Hong Ooi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!