Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generating a vector of difference between two vectors

Tags:

r

I have two csv files, and each of which consists of one column of data

For instance, vecA.csv is like

id 1 2 

vecB.csv is like

id 3 2 

I read the data set as follows:

vectorA<-read.table("vecA.csv",sep=",",header=T) vectorB<-read.table("vecB.csv",sep=",",header=T) 

I want to generate a vector consisting of elements belonging to B only.

like image 542
user785099 Avatar asked Feb 19 '13 04:02

user785099


People also ask

How do you find the difference between two vectors in R?

Method 1: Using setdiff() method The setdiff() method in R is used to retrieve the elements of vector X, which are not contained in Y. This method can be applied where the two vectors may belong to different data types, as well, where the elements of the first argument vector are returned unmodified.

What is a difference vector?

A vector difference is the result of subtracting one vector from another. A vector difference is denoted using the normal minus sign, i.e., the vector difference of vectors and is written .

How do I check if two vectors match in R?

Check if Two Objects are Equal in R Programming – setequal() Function. setequal() function in R Language is used to check if two objects are equal. This function takes two objects like Vectors, dataframes, etc. as arguments and results in TRUE or FALSE, if the Objects are equal or not.


2 Answers

You are looking for the function setdiff

setdiff(vectorB$id, vectorA$id) 

If you did not want this reduced to unique values, you could create a not in function

(kudos to @joran here Match with negation)

'%nin%' <- Negate('%in%')  vectorB$id[vectorB$id %nin% vectorA$id] 
like image 64
mnel Avatar answered Oct 11 '22 08:10

mnel


If your vector's are instead data.tables, then all you need are five characters:

B[!A] 

library(data.table)  # read in your data, wrap in data.table(..., key="id")  A <- data.table(read.table("vecA.csv",sep=",",header=T), key="id") B <- data.table(read.table("vecB.csv",sep=",",header=T), key="id")  # Then this is all you need B[!A] 

[Matthew] And in v1.8.7 it's simpler and faster to read the file as well :

A <- setkey(fread("vecA.csv"), id) B <- setkey(fread("vecB.csv"), id) B[!A] 
like image 22
Ricardo Saporta Avatar answered Oct 11 '22 08:10

Ricardo Saporta