You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何比较两个DataFrame字符型列并找出df1独有的唯一字符串

找出仅存在于df1$text中的唯一字符串

数据定义

df1 = data.frame("text" = c("fht49_169_fcxrtvf", "fht49_169_fcxrtvf","fht15_20_trddrefx", "fht15_20_trddrefx", "fht15_18_trddrefx", "fht28_18_ptybrwdf", "fht1_1_tyhfvcs", "fht33_151_tyhfvcs", "fht20_14_gfhyqrsx"), 
                  "values_x" = c(3,55,11,3,5,4,8,33,1))
df2 = data.frame("text" = c("fht49_169_fcxrtvf", "fht15_18_trddrefx", "fht28_18_ptybrwdf", "fht33_151_tyhfvcs"), 
                 "values_x" = c(3,11,3,5))   

df1数据表

textvalues_x
fht49_169_fcxrtvf3
fht49_169_fcxrtvf55
fht15_20_trddrefx11
fht15_20_trddrefx3
fht15_18_trddrefx5
fht28_18_ptybrwdf4
fht1_1_tyhfvcs8
fht33_151_tyhfvcs33
fht20_14_gfhyqrsx1

df2数据表

textvalues_x
fht49_169_fcxrtvf3
fht15_18_trddrefx11
fht28_18_ptybrwdf3
fht33_151_tyhfvcs5

解决方法

用unique()提取两表text列的唯一值,再通过setdiff()筛选出仅存在于df1中的字符串:

# 计算并保存结果
result <- setdiff(unique(df1$text), unique(df2$text))

# 打印结果
print(result)

运行结果

[1] "fht15_20_trddrefx" "fht1_1_tyhfvcs"    "fht20_14_gfhyqrsx"

内容的提问来源于stack exchange,提问作者dede

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 23:35:09