Removing data from one dataframe that exists in another dataframe R

前端 未结 3 597
一整个雨季
一整个雨季 2021-02-10 03:31

I want to remove data from a dataframe that is present in another dataframe. Let me give an example:

letters<-c(\'a\',\'b\',\'c\',\'d\',\'e\')
numbers<-c(1         


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-02-10 03:58

    Answer is response to your edit: " so I really can't use the negative expression".

    I guess one of the most efficient ways to do this is using data.table as follows:

    require(data.table)
    setDT(list_one)
    setDT(list_two)
    list_one[!list_two, on=c(letters = "letters2")]
    

    Or

    require(data.table)
    setDT(list_one, key = "letters")
    setDT(list_two, key = "letters2")
    list_one[!letters2]
    

    (Thanks to Frank for the improvement)

    Result:

       letters numbers
    1:       b       2
    2:       e       5
    

    Have a look at ?"data.table" and Quickly reading very large tables as dataframes in R on why to use data.table::freadto read the csv-files in the first place.

    BTW: If you have letters2 instead of list_two you can use

    list_one[!J(letters2)]
    

提交回复
热议问题