英文:
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 %>%
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论