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

如何查找两个DataFrame中name与artistName列的共同值组合

查找两个DataFrame指定列取值均相同的记录

针对匹配两个表中artistName和name两列取值完全一致的记录的需求,可通过以下R语言方案实现:

示例数据

dat1<-structure(list(artistName = c("Adele", "Mariah Carey", "D-Block Europe", "Wham!", "Ed Sheeran", "Adele", "Adele", "Elton John & Dua Lipa", "ArrDee", "GAYLE", "Ed Sheeran", "The Pogues", "Ed Sheeran", "Shakin' Stevens", "Leona Lewis", "Sam Fender", "Acraze", "Kelly Clarkson", "Joel Corry", "SwitchOTR"), name = c("Easy On Me", "All I Want For Christmas Is You", "Overseas (feat. Central Cee)", "Last Christmas", "Shivers", "Oh My God", "I Drink Wine", "Cold Heart (PNAU Remix)", "Flowers (Say My Name)", "abcdefu", "Bad Habits", "Fairytale of New York", "Overpass Graffiti", "Merry Christmas Everyone", "One More Sleep", "Seventeen Going Under", "Do It To It (feat. Cherish)", "Underneath the Tree", "I Wish (feat. Mabel)", "Coming for You (feat. A1 x J1)")), row.names = c(NA, 20L), class = "data.frame")

dat2<-structure(list(artistName = c("Adele", "Rod Wave", "Kodak Black", "Drake", "Nardo Wick", "Drake", "Adele", "Bruno Mars, Anderson .Paak & Silk Sonic", "Adele", "Summer Walker & SZA", "Mariah Carey", "GAYLE", "Drake", "Doja Cat", "Lil Nas X & Jack Harlow", "Taylor Swift", "The Kid LAROI & Justin Bieber", "Adele", "Summer Walker", "Drake"), name = c("Easy On Me", "By Your Side", "Super Gremlin", "Knife Talk (feat. 21 Savage & Project Pat)", "Who Want Smoke?? (feat. G Herbo, Lil Durk & 21 Savage)", "Way 2 Sexy (feat. Future & Young Thug)", "Oh My God", "Smokin Out The Window", "My Little Love", "No Love", "All I Want For Christmas Is You", "abcdefu", "Girls Want Girls (feat. Lil Baby)", "Need To Know", "INDUSTRY BABY", "All Too Well (10 Minute Version) (Taylor's Version) (From The Vault)", "STAY", "I Drink Wine", "Insane", "Fair Trade (feat. Travis Scott)")), row.names = c(NA, 20L), class = "data.frame")

实现方法

  • 基础R原生方案:使用merge函数做内连接,无需额外安装依赖,直接指定匹配的列即可:
common_records <- merge(dat1, dat2, by = c("artistName", "name"))
  • dplyr包方案:适合tidyverse生态用户,语法更简洁,支持管道操作:
library(dplyr)
common_records <- inner_join(dat1, dat2, by = c("artistName", "name"))

两种方案返回的结果均为两表中artistName和name取值完全相同的记录,上述示例数据运行后会得到5条匹配结果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 10:45:05