r - 如何根据列中给出的对象名称和代码创建新列
问题描述
我在 Excel 中有以下数据。我已经进口了。
Item_code Price Raw_Material
1. 10001jk10002 20 Made with Apple
2. 10001jk10002 20 Made with Grapes
3. 10001jk10002 30 Made with Banana
4. 10011jk10022 60 Made with Grapes
5. 10011jk10022 60 Made with Grapes
我正在寻找新列的结果
Item_code Price Raw_Material Fruit Used
1. 10001jk10002 20 Made with Apple Apple
2. 10001jk10002 20 Made with Grapes Grapes
3. 10001jk10002 30 Made with Banana Banana
4. 10011jk10022 60 Made with Grapes Grapes
5. 10011jk10022 60 Made with Grapes Grapes
从新专栏我想再开一篇新专栏“Final Fruite”
Item_code Price Raw_Material Fruit Used Final Fruit
1. 10001jk10002 20 Made with Apple Apple Banana
2. 10001jk10002 20 Made with Grapes Grapes Banana
3. 10001jk10002 30 Made with Banana Banana Banana
4. 10011jk10022 60 Made with Grapes Grapes Grapes
5. 10011jk10022 60 Made with Grapes Grapes Grapes
如果你能看到我的前 3 行是一样的。首先,我想基于 Raw_material 列来驱动 Fruit 列。句子中使用了水果名称(可以是随机的),然后我想从水果列中导出另一列,Final_Fruite
无论下一行是什么水果我想在我的新列中返回香蕉
首选水果的实际列表为 10。我正在寻找动态解决方案。谁能建议我如何做同样的事情来获得期望的结果。
解决方案
library(readxl)
library(dplyr)
library(magrittr)
library(stringr)
fruity <- read_excel("fruity.xlsx")
fruity <- fruity %>%
group_by(item_code) %>%
mutate(id = row_number()) %>%
mutate(fruit_used = word(raw_material, -1))
tmp <- fruity %>% group_by(item_code) %>% top_n(1, id) %>%
select(item_code, fruit_used) %>%
set_colnames(c('item_code','final_fruit'))
fruity <- fruity %>% left_join(tmp, by = 'item_code') %>% select(-"id")
推荐阅读
- sbt - 为什么项目中有两个不同版本的sbt
- parameters - verilog/systemverilog 通过生成的模块层次结构向上传递参数
- javascript - 如何将 React Native AsyncStorage 与 Redux 一起使用?
- google-cloud-platform - Run node.js database migrations on Google Cloud SQL during Google Cloud Build
- session - 单页应用程序、服务器端会话和页面加载
- c++ - 我如何告诉 em++ 找到 WS2tcpip.h
- react-native - 如何在本机反应中固定元素
- python - 分配前引用的局部变量“responsedata”(使用请求库)
- python - 特定数量的网格点的 matplotlib 流图错误
- c# - 在主类的子类列表值中设置子项