Is there a way to conditionally concotanate and add two rows while creating a third row that combines elements?

huangapple go评论56阅读模式
英文:

Is there a way to conditionally concotanate and add two rows while creating a third row that combines elements?

问题

假设我有以下内容

项目 配料 成本 项目ID
比萨 奶酪 $.01 001
比萨 面粉 $.04 001
比萨 番茄 $.04 001
面包 面粉 $.04 003
饼干 面粉 $.07 004
三明治 面粉 $.04 002
三明治 奶酪 $.01 002
三明治 $.10 002
三明治 生菜 $.01 002

我应该如何创建新行,将配料合并并计算成本总额,同时保留项目和项目ID不变?

我的主要问题是新行的放置,我希望它们位于单独的配料上方。我无法想象创建一个循环来处理这个问题。是否有一个优雅的解决方案?

项目 配料 成本 项目ID
比萨 番茄 奶酪 面粉 $.13 001
比萨 奶酪 $.01 001
比萨 面粉 $.04 001
比萨 番茄 $.07 001
面包 面粉 $.04 003
饼干 面粉 $.07 004
三明治 面粉 奶酪 肉 生菜 $.16 002
三明治 面粉 $.04 002
三明治 奶酪 $.01 002
三明治 $.10 002
三明治 生菜 $.01 002
英文:

Suppose I had the following

Item Ingredients Cost Item ID
Pizza Cheese $.01 001
Pizza Flour $.04 001
Pizza Tomato $.04 001
Bread Flour $.04 003
Cookies Flour $.07 004
Sandwich Flour $.04 002
Sandwich Cheese $.01 002
Sandwich Meat $.10 002
Sandwich Lettuce $.01 002

How would I create new rows that concatenate the ingredients and add up the total item cost in Cost all while leaving item and item id alone?

My main issue is the placement of the new rows as I would want them to be on top of the individual ingredients. I can't wrap my head on creating a loop for this. Is there an elegant solution?

Item Ingredients Cost Item ID
Pizza Tomato Cheese Flour $.13 001
Pizza Cheese $.01 001
Pizza Flour $.04 001
Pizza Tomato $.07 001
Bread Flour $.04 003
Cookies Flour $.04 004
Sandwich Flour Cheese Meat Lettuce $.16 002
Sandwich Flour $.04 002
Sandwich Cheese $.01 002
Sandwich Meat $.10 002
Sandwich Lettuce $.01 002

答案1

得分: 1

我们可以执行

```R
library(dplyr)
library(stringr)
df1 %>%
   reframe(Ingredients = str_c(Ingredients, collapse = ' '), 
  Cost = str_c('$', sum(readr::parse_number(as.character(Cost)), na.rm = TRUE)), 
       .by = c('Item', 'Item ID')) %>%
  bind_rows(., df1 %>% mutate(Cost = str_c("$", Cost)) %>%
  arrange(Item)
英文:

We may do

library(dplyr)
library(stringr)
df1 %>%
   reframe(Ingredients = str_c(Ingredients, collapse = ' '), 
  Cost = str_c('$', sum(readr::parse_number(as.character(Cost)), na.rm = TRUE)), 
       .by = c('Item', 'Item ID')) %>%
  bind_rows(., df1 %>% mutate(Cost = str_c("$", Cost)) %>%
  arrange(Item)

</details>



# 答案2
**得分**: 0

以下是翻译好的部分:

这里是一个备选的 `tidyverse` 解决方案:

```R
df %>%
  group_by(Item) %>%
  summarise(Ingredients = str_c(Ingredients, collapse = " "),
            Costs = str_c("$", sum(as.numeric(str_extract(Costs, "(?<=\$).*"))))) %>%
  mutate(rowID = 1) %>%
  bind_rows(., df %>% mutate(rowID = row_number()+1)) %>%
  group_by(Item) %>%
  arrange(Item) %>%
  select(-rowID)
英文:

Here's an alternative tidyverse solution:

df %&gt;%
  group_by(Item) %&gt;%
  summarise(Ingredients = str_c(Ingredients, collapse = &quot; &quot;),
            Costs = str_c(&quot;$&quot;, sum(as.numeric(str_extract(Costs, &quot;(?&lt;=\$).*&quot;))))) %&gt;%
  mutate(rowID = 1) %&gt;%
  bind_rows(., df %&gt;% mutate(rowID = row_number()+1)) %&gt;%
  group_by(Item) %&gt;%
  arrange(Item) %&gt;%
  select(-rowID)

huangapple
  • 本文由 发表于 2023年2月24日 02:12:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定