简体   繁体   English

如何根据不同数据帧的两个ID列的匹配从数据帧列中提取值?

[英]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.

相关问题 匹配来自两个数据框的两列,并提供不同的列 - Match two columns from two dataframes and provide different column 根据其他两个数据帧中唯一的值提取数据帧的子集 - Extract the subset of a dataframe based with values unique from other two dataframes 查找在两个数据帧中匹配的两列,并使用 R 将数据帧 2 中的第三列放入数据帧 1 中的新列中 - Find two columns that match in two dataframes and put third column from dataframe 2 into a new column in dataframe 1 using R 如何匹配2个数据框列并提取列值和列名? - How to match 2 dataframe columns and extract column values and column names? 如何基于两个无序列合并两个数据框的列值 - How to merge the column values of two dataframes based on two unordered columns 如何匹配两个不同表的列中的值以提取 R 中第二个表的不同列中的对应值? - How do I match values in columns of two different tables to extract corresponding values in a different column of the second table in R? 如何提取比较两个不同数据帧的两列的列? - How can I extract a column that compares two columns of two different dataframes? 如何通过匹配 R 中的其他两列将列中的值提取到数据框中 - How to extract values from a column into the dataframe by matching two other columns in R 按id匹配并在两个数据帧之间划分列值 - Match by id and divide column values across two dataframes 如何从 R 中的两个不同数据帧添加两列,其中一列只有另一列的唯一值的子集 - How to add two columns from two different dataframes in R wherein one column just has subset of unique values of the other
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM