Remove a subset of records from a dataframe in r

10,868

We can use anti_join

library(dplyr)
anti_join(df, another_df)

Or if this is based on the rownames, then %in% can be used for creating a logical index to subset the rows

df[!row.names(df) %in% row.names(another_df),]
Share:
10,868
SaikiHanee
Author by

SaikiHanee

Updated on June 14, 2022

Comments

  • SaikiHanee
    SaikiHanee almost 2 years

    We can combine 2 dataframes using df = rbind(df, another_df). How it should be if its required to remove another_df from df where rownames of df and another_df are not matching.

    df = data.frame(A=c('a','aa','aaa'), B=c('b','bb','bbb'))
    rownames(df)
    
    another_df =data.frame(A=c('aa','a'), B=c('bb','b'))
    rownames(another_df)=c('3','4')