【问题标题】:R programming: ifelse on multiple tables [duplicate]R编程:多个表上的ifelse
【发布时间】:2018-06-06 13:08:48
【问题描述】:

我只是冒险进入 R 编程并找到自己的方式。

假设我有一张如下表:

Store | Product | Sales
X | A | 2
X | B | 1
X | C | 3
Y | A | 1
Y | B | 2
Y | C | 5
Z | A | 3
Z | B | 6
Z | C | 2

我需要根据另一个表更改某些产品的销售值。请在下面找到:

Product | Sales
A | 10
B | 7
C | 15

我的决赛桌应该是:

Store | Product | Sales
X | A | 10
X | B | 7
X | C | 15
Y | A | 10
Y | B | 7
Y | C | 15
Z | A | 10
Z | B | 7
Z | C | 15

我现在有两种方法可以做到这一点: 1) 使用连接 2) 在 for 循环中使用 if-else 语句来子集

有没有其他方法可以更有效地减少步骤? 提前致谢!

编辑:我之前忘了提到一个例外。如果我的数据集如下所示怎么办?

商店 |产品 |销售量
X |一个 | 2
X |乙| 1
X | C | 3
X | D | 4
是 |一个 | 1
是 |乙| 2
是 | C | 5
是 | D | 2
Z |一个 | 3
Z |乙| 6
Z | C | 2
Z | D | 3

有一个额外的产品 (D) 与销售。如果该产品不存在于第二个表中,我想保留该产品的销售价值,即:

产品 |销售量 一个 | 10 乙| 7 C | 15

【问题讨论】:

  • “我现在有两种方法”请分享你的代码。
  • 使用连接听起来不错。你也可以使用match
  • @LAP 不是真的,c(A = 10, B = 7, C = 15) 可以使用现有数据框生成。 OP 想要“其他”解决方案,但我会选择加入/匹配解决方案。
  • df1$SalesNew <- df2$Sales[match(df1$Product, df2$Product)] 将按照@Gregor 的建议工作。
  • 我投票结束,因为我不明白为什么您现有的 2 种方法不够好,帖子“太宽泛”。

标签: r for-loop if-statement join apply


【解决方案1】:

这个join怎么样?

由于您只想更改某些ProductsSales 值,为了说明这一点,我只考虑了lookup_df 中的两个产品

library(dplyr)

df %>%
  left_join(lookup_df, by = "Product", suffix = c("_Original", "_New")) %>%
  mutate(Sales_New = coalesce(Sales_New, Sales_Original))

输出为:

  Store Product Sales_Original Sales_New
1     X       A              2        10
2     X       B              1         1
3     X       C              3        15
4     Y       A              1        10
5     Y       B              2         2
6     Y       C              5        15
7     Z       A              3        10
8     Z       B              6         6
9     Z       C              2        15

样本数据:

df <- structure(list(Store = c("X", "X", "X", "Y", "Y", "Y", "Z", "Z", 
"Z"), Product = c("A", "B", "C", "A", "B", "C", "A", "B", "C"
), Sales = c(2L, 1L, 3L, 1L, 2L, 5L, 3L, 6L, 2L)), .Names = c("Store", 
"Product", "Sales"), class = "data.frame", row.names = c(NA, 
-9L))

lookup_df <- structure(list(Product = c("A", "C"), Sales = c(10L, 15L)), .Names = c("Product", "Sales"), class = "data.frame", row.names = c(NA, 
-2L))
#  Product Sales
#1       A    10
#2       C    15

【讨论】:

  • 嗨。感谢您的帮助。我对这个问题做了一个小修改,但有一个例外。
  • 我已经考虑过您的更新案例。如果您仔细查看我的答案中使用的示例数据,那么在lookup_df 产品B 中不存在,但在df 中它具有销售价值。
  • @Adithyashankar 您可能还想查看link
【解决方案2】:

如果使用查找向量,则相对较短:

d <- read.table(text = "
Store | Product | Sales
X | A | 2
X | B | 1
X | C | 3
Y | A | 1
Y | B | 2
Y | C | 5
Z | A | 3
Z | B | 6
Z | C | 2", sep = "|", header = T, stringsAsFactors = F)

lookup <- read.table(text = "Product | Sales
A | 10
B | 7
C | 15", sep = "|", header = T, stringsAsFactors = F)

lookup$Product <- gsub("^\\s+|\\s+$", "", lookup$Product) # remove spaces
lookup <- setNames(lookup$Sales, lookup$Product) # convert to vector
d$Product <- gsub("^\\s+|\\s+$", "", d$Product) # remove spaces

d$Sales <- lookup[d$Product] # main part
d

【讨论】:

  • 嗨。感谢您的帮助。我对这个问题做了一个小修改,但有一个例外。
猜你喜欢
  • 2019-05-06
  • 1970-01-01
  • 2022-07-07
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-28
相关资源
最近更新 更多