简体   繁体   English

R - 匹配两个数据帧中的两列

[英]R - Match two columns in two dataframes

I'm trying to manipulate a dataframe where certain conditions are met.我正在尝试操作满足某些条件的 dataframe。

There's the main dataframe df1:主要有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: df2:

Label       Title            Artist        Operation
Warner      Nicotine         Chef`Special  0
Sony        Break my Heart   Dua Lipa      1
... 100+ rows

Df1 contains some mistakes. Df1 包含一些错误。 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.在这种情况下,尼古丁的 Warner 值为 1,而它应该为 0。我希望从两个数据帧中匹配 Title 和 Artist,然后根据 df2 将 label 值更改为 1 或 0。

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.在这种情况下,它应该看到 Chef'Special 的尼古丁在 df1 和 df2 中,它应该将 df1$Warner 更改为 0。 Dua Lipa 的 Break My Heart 应该使用相同的方法将 df1$Sony 设置为 1。

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:这是使用合并的一个选项,没有您的数据,因此以mtcars为例:

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)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM