使用行中的值匹配另一个数据框中的列和行

时间:2018-09-05 00:24:03

标签: r dataframe match

我有一个数据框df1,其中包含这样的产品列表:

| SKU | Product | Model |  Size  |
|-----|---------|-------|--------|
|   1 | X       | A     | Small  |
|   2 | X       | B     | Large  |
|   3 | X       | B     | Medium |
...

还有一个数据框df2,其中包含每个型号和尺寸的价格,如下所示:

| Model | Small | Medium | Large |
|-------|-------|--------|-------|
| A     |    10 |     12 |    15 |
| B     |     8 |      9 |    10 |
| C     |     7 |      8 |    12 |
| D     |    12 |     13 |    14 |
...

我想要的是将df1中的产品型号和尺寸与df2中的数据进行匹配,以便我可以在第一个数据框中添加带有价格的新列。 结果应为以下内容:

| SKU | Product | Model |  Size  | Price |
|-----|---------|-------|--------|-------|
|   1 | X       | A     | Small  |    10 |
|   2 | X       | B     | Large  |    10 |
|   3 | X       | B     | Medium |     9 |
...

如何使用R做到这一点?

2 个答案:

答案 0 :(得分:1)

以下是一种方法。您可以重塑df2并创建一个名为Price的新列。然后,您合并数据。

library(dplyr)
library(tidyr)

df1 <- data.frame(SKU = 1:3,
                  Product = c("X", "X", "X"),
                  Model = c("A", "B", "B"),
                  Size = c("Small", "Large", "Medium"),
                  stringsAsFactors = FALSE)

df2 <- data.frame(Model = LETTERS[1:4],
                  Small = c(10, 8, 7, 12),
                  Medium = c(12, 9, 8, 13),
                  Large = c(15, 10, 12, 14),
                  stringsAsFactors = FALSE)

gather(df2, key = Size, value = Price, -Model) %>%
      left_join(df1, ., by = c("Model", "Size")) 
  SKU Product Model   Size Price
1   1       X     A  Small    10
2   2       X     B  Large    10
3   3       X     B Medium     9

答案 1 :(得分:1)

这是使用基数R的解决方案:

Jazzurro的数据:

df1 <- data.frame(SKU = 1:3,
                  Product = c("X", "X", "X"),
                  Model = c("A", "B", "B"),
                  Size = c("Small", "Large", "Medium"),
                  stringsAsFactors = FALSE)

df2 <- data.frame(Model = LETTERS[1:4],
                  Small = c(10, 8, 7, 12),
                  Medium = c(12, 9, 8, 13),
                  Large = c(15, 10, 12, 14),
                  stringsAsFactors = FALSE)

1个班轮:

df1$Price <- apply(df1, 1, function(x) df2[df2[,"Model"] %in% x["Model"],x["Size"]] )
#  SKU Product Model   Size Price
#1   1       X     A  Small    10
#2   2       X     B  Large    10
#3   3       X     B Medium     9