给出两个文件
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
仅保留两个文件中存在名称的行。例如,期望的输出:
档案1
Value name
7 nicola
文件2
Value name
0.8 nicola
答案 0 :(得分:1)
我们可以尝试
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