generating a vector of difference between two vectors

后端 未结 2 724
梦谈多话
梦谈多话 2020-12-04 21:58

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

2条回答
  •  心在旅途
    2020-12-04 22:19

    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]
    

提交回复
热议问题