[英]How to extract values from a dataframe column based on the match of two ID columns of different dataframes?
I'm having a little trouble going about this. 我在解决这个问题上遇到了一些麻烦。 Basically, I want to extract the values from column MakeText based on the first occurrence of an id match of 2 different dataframes. 基本上,我想基于第一次出现2个不同数据帧的id匹配从MakeText列中提取值。 One dataframe has only unique IDs, whereas the other dataframe has multiple rows of that same ID, hence the clause for the first occurrence. 一个数据帧只有唯一的ID,而另一个数据帧有多个具有相同ID的行,因此是第一次出现的子句。 Each unique ID has unique values for MakeText column. 每个唯一ID都具有MakeText列的唯一值。
I tried match and merge in r but I have no idea how to extract column values in case of it due to the varying number of rows of the dataframes. 我在r中尝试了匹配和合并,但由于数据帧的行数不同,我不知道如何提取列值。
Based on the id of this: 基于此ID:
wb <- spread(data = newdata,
key = an,
value = av)
names(wb)[1] <- "ID"
wb <- as.data.frame(wb)
View(wb)
Id BodyColorText
1.0 blue
100.0 cyan
1001.0 red
1003.0 black
I want to match it to the ID of this: 我想将它与此ID相匹配:
View(supplierdata)
Id MakeText
1.0 Mercedes
1.0 Mercedes
1001.0 Ferrari
1001.0 Ferrari
1003.0 Audi
and extract the MakeText column values to concatenate it to the wb dataframe with respect to that ID. 并提取MakeText列值以将其连接到与该ID相关的wb数据帧。
As the number of rows are not equal - wb has 1103 while supplierdata has 20957 - I am finding it hard to do this. 由于行数不相等 - wb有1103而supplierdata有20957 - 我发现很难做到这一点。
The final output that I am looking for is the 'wb' dataframe to look like: 我正在寻找的最终输出是'wb'数据框,如下所示:
Id BodyColorText MakeText
1.0 blue Mercedes
100.0 cyan Porsch
1001.0 red Ferrari
1003.0 black Audi
Help will be much appreciated. 将非常感谢帮助。
Using dplyr
package : 使用dplyr
包:
inner_join(wb, distinct(supplierdata), by = 'Id')
Is that what you are looking for ? 那是你在找什么?
I believe you're looking for left_join
. 我相信你在寻找left_join
。
library(dplyr)
left_join(wb, suplierdata)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.