首页 > 解决方案 > 如何根据列中给出的对象名称和代码创建新列

问题描述

我在 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。我正在寻找动态解决方案。谁能建议我如何做同样的事情来获得期望的结果。

标签: r

解决方案


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")

推荐阅读