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

问题描述 投票:0回答:2

我有一个数据框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做到这一点?

r dataframe match
2个回答
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
投票

这是一个使用基数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
© www.soinside.com 2019 - 2024. All rights reserved.