r - R - Match two columns in two dataframes
问题描述
I'm trying to manipulate a dataframe where certain conditions are met.
There's the main dataframe df1:
Title Artist Warner Sony Universal
Break My Heart Dua Lipa 0 0 0
Daisies Katy Perry 0 0 1
Nicotine Chef`Special 1 1 0
... 1 000 000+ rows
df2:
Label Title Artist Operation
Warner Nicotine Chef`Special 0
Sony Break my Heart Dua Lipa 1
... 100+ rows
Df1 contains some mistakes. In this case Nicotine has Warner value 1 while it should be 0. I'm looking to match Title and Artist from both dataframes and then changing the label value to either 1 or 0 depending on df2.
In this instance it should see that Nicotine by Chef'Special is in df1 and df2, and it should change df1$Warner to 0. Break My Heart by Dua Lipa should have df1$Sony set to 1 using the same method.
I have been thinking about how to tackle this for quite some time but I'm at a complete loss.
解决方案
Here is one option using merge, didn't have your data so used mtcars
as an example:
df1 = head(mtcars, 5)
df2 = subset(head(within(df1,mpg <- mpg * 2),2), select = "mpg")
df3 <- merge(df1, df2, by = 0, all.x = TRUE)
df3 <- within(df3, mpg <- ifelse(is.na(mpg.y),
mpg.x, mpg.y))[-(2:3)]
推荐阅读
- python - 有没有办法删除电子邮件地址中@之后的字符?
- python - PyCharm:如何在本地禁用缺少文档字符串检查
- c# - 用户或管理员的 ASP.Net Core WebAPI 授权策略
- excel - “第一个星期一”公式的Excel变体
- c++ - Visual Studio 2015 中的外部“C”显式类型错误,DLL 测试代码
- python - 如何让用户名成为计算机可以使用和读取的变量?
- visual-studio-code - VS Code API 获取右括号的位置
- twitter-bootstrap - 为什么单击到其他选项卡后第一个导航选项卡的内容会被替换?
- html - 有没有更简单的方法来检查表单中的密码和重复密码是否匹配?
- python - 如何修复“在此服务器上找不到请求的 URL”apache2 + django + ec2?