-1

From this question Matching multiple columns on different data frames and getting other column as result

If I want to select a row which doesn't match in multiple column from two dataframe.

output <- merge(df1, df2, by.x='init', by.y='V2')

result <- df2[-output,] This code doesn't work.

The expect output is a row from df2 which doese not match in output. The expect output should be

 V1    V2     V3

10  1     69094 medium
11  1     69094 medium
13  1     69095 medium

Thank you for all answer

Community
  • 1
  • 1
  • In the example provided, both `df1` and `output` have the same rows except that output has two additional columns. So, it is not clear what your expected output is. – akrun Sep 13 '14 at 13:46
  • I assume that you might be referring to `df2`. `subset(merge(df2, output, by.x=c("V1", "V2", "V3"), by.y=c("V1", "init", "V3"), all=TRUE), is.na(chr), select=1:3)` or you could use `anti_join` from `dplyr`. – akrun Sep 13 '14 at 13:55
  • I think I ask a question is not clear so I will modify my question @ akrun – user3917101 Sep 13 '14 at 14:02
  • Please show the expected output – akrun Sep 13 '14 at 14:02
  • If you don't need the row numbers, the `subset` option I mentioned should match the expected result. – akrun Sep 13 '14 at 14:21

1 Answers1

0

You could try:

  indx <- as.character(interaction(df2)) %in% 
                as.character(interaction(output[,c(3,1,4)]))

  df2[!indx,]
  #  V1    V2     V3
  #10  1 69094 medium
  #11  1 69094 medium
  #13  1 69095 medium

If row numbers are not important, either:

  subset(merge(df2, output, by.x=c("V1", "V2", "V3"), by.y=c("V1", "init", "V3"), all=TRUE), is.na(chr), select=1:3)

Or

  library(dplyr)
   df1New <- df1
   colnames(df1New)[2] <- "V2"
   anti_join(df2, df1New, by="V2")
  #   V1    V2     V3
  #1  1 69095 medium
  #2  1 69094 medium
  #3  1 69094 medium
akrun
  • 874,273
  • 37
  • 540
  • 662