-2

given two files

df1

df1=read.table("C:\\file1.txt", sep="")
Value   name
  0.2    sam
  0.4  david
  3    david
  7    nicola

df2

File2=read.table("C:\\file2.txt", sep="")
Value   name
  0.8 nicola
  0.9  david

Keep only rows where names exist in both files. For instance, desired outputs:

File 1

Value   name
  7  nicola

File 2

Value   name
  0.8  nicola
temor
  • 935
  • 2
  • 10
  • 26

1 Answers1

1

We can try

tbl1 <- table(df1$name)
tbl2 <- table(df2$name)
i1 <- names(tbl1) %in% names(tbl2)
nm1 <- names(tbl1)[tbl1[i1]==tbl2[i1]]
df1[df1$name %in% nm1,]
#  Value   name
#4     7 nicola

i2 <- names(tbl2) %in% names(tbl1)
i21 <- tbl2[i2]==tbl1[intersect(names(tbl1), names(tbl2[i2]))]

nm2 <- names(i21)[i21]
df2[df2$name %in% nm2,]
#   Value   name
#1   0.8 nicola
akrun
  • 874,273
  • 37
  • 540
  • 662
  • @temor I haven't tested it. But, hope so. – akrun Dec 08 '15 at 07:01
  • @temor My code was based on the example provided. You can try similar to the second code block – akrun Dec 09 '15 at 09:08
  • @temor I mean, try to do the same as I have done with `i2 <-` or if you can show some small example that gives the error, I can change the code. – akrun Dec 09 '15 at 09:14
  • @temor Please do update the new data iin your post so that I can test it. – akrun Dec 09 '15 at 09:32