如何在R中创建数据框的路由

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

How to create a route in a dataframe with R

问题

以下是翻译好的部分:

我正在尝试在R中映射数据帧中的路线。
整个下午都在尝试做这个...

这是我的数据看起来的样子,我生成了一个简单的数据集,以保密为由。

如何在R中创建数据框的路由

  1. end_point <- c("DC_Frankfurt","F6_DC_Bordeaux","B3_Paris","BEAG_Toronto","DC_Frankfurt")
  2. product_code <- c("000001","000001","000001","000002","000002")
  3. site <- c("Plant1","DC_Frankfurt","DC_Frankfurt","DC_Frankfurt","Plant2")
  4. step <- c("step1","step2","step2","step2","step1")
  5. df <- data.frame(end_point, product_code,site,step)

这是我希望它看起来的样子:

如何在R中创建数据框的路由

  1. product_code <- c("000001","000001","000002")
  2. step1 <- c("Plant1","Plant1","Plant2")
  3. step2 <- c("DC_Frankfurt","DC_Frankfurt","DC_Frankfurt")
  4. end_point <- c("F6_DC_Bordeaux","B3_Paris","BEAG_Toronto")
  5. result_expected <- data.frame(product_code,step1,step2,end_point)

我已经尝试了这段代码,但是没有结果:

  1. my_df_test <- df %>%
  2. pivot_wider(names_from = step,
  3. values_from = site)

谢谢大家!

英文:

I am trying to map routes in a dataframe in R.
Spent the whole afternoon trying to do it...

Here is how my data looks like, i generated a simple dataset because of confidentiality.

如何在R中创建数据框的路由

  1. end_point <- c("DC_Frankfurt","F6_DC_Bordeaux","B3_Paris","BEAG_Toronto","DC_Frankfurt")
  2. product_code <- c("000001","000001","000001","000002","000002")
  3. site <- c("Plant1","DC_Frankfurt","DC_Frankfurt","DC_Frankfurt","Plant2")
  4. step <- c("step1","step2","step2","step2","step1")
  5. df <- data.frame(end_point, product_code,site,step)

Here is what I would like it to be :

如何在R中创建数据框的路由

  1. product_code <- c("000001","000001","000002")
  2. step1 <- c("Plant1","Plant1","Plant2")
  3. step2 <- c("DC_Frankfurt","DC_Frankfurt","DC_Frankfurt")
  4. end_point <- c("F6_DC_Bordeaux","B3_Paris","BEAG_Toronto")
  5. result_expected <- data.frame(product_code,step1,step2,end_point)

I tried this piece of code already but it is a dead-end:

  1. my_df_test <- df %>%
  2. pivot_wider(names_from = step,
  3. values_from = site)

Thank you all!

答案1

得分: 1

你可以将数据分成步骤1和步骤2,然后执行左连接:

  1. library(tidyverse)
  2. df %>%
  3. filter(step == "step2") %>%
  4. rename(step2 = site) %>%
  5. select(-step) %>%
  6. left_join(df %>%
  7. filter(step == "step1") %>%
  8. rename(step1 = site) %>%
  9. select(-step, -end_point), by = "product_code") %>%
  10. select(product_code, step1, step2, end_point)
  11. #> product_code step1 step2 end_point
  12. #> 1 000001 Plant1 DC_Frankfurt F6_DC_Bordeaux
  13. #> 2 000001 Plant1 DC_Frankfurt B3_Paris
  14. #> 3 000002 Plant2 DC_Frankfurt BEAG_Toronto

创建于2023-02-08,使用reprex v2.0.2

英文:

You could split the data into step 1 and step 2, then perform a left join:

  1. library(tidyverse)
  2. df %>%
  3. filter(step == "step2") %>%
  4. rename(step2 = site) %>%
  5. select(-step) %>%
  6. left_join(df %>%
  7. filter(step == "step1") %>%
  8. rename(step1 = site) %>%
  9. select(-step, -end_point), by = "product_code") %>%
  10. select(product_code, step1, step2, end_point)
  11. #> product_code step1 step2 end_point
  12. #> 1 000001 Plant1 DC_Frankfurt F6_DC_Bordeaux
  13. #> 2 000001 Plant1 DC_Frankfurt B3_Paris
  14. #> 3 000002 Plant2 DC_Frankfurt BEAG_Toronto

<sup>Created on 2023-02-08 with reprex v2.0.2</sup>

答案2

得分: 1

我们可以使用 pivot_widerfill 完成这个操作。

  1. library(dplyr)
  2. library(tidyr)
  3. df %>%
  4. pivot_wider(names_from = 'step', values_from = 'site') %>%
  5. fill(starts_with('step'), .direction = 'downup') %>%
  6. filter(step2 != end_point) %>%
  7. relocate(end_point, .after = last_col())

-输出

  1. # 一个 tibble: 3 x 4
  2. product_code step1 step2 end_point
  3. 1 000001 Plant1 DC_Frankfurt F6_DC_Bordeaux
  4. 2 000001 Plant1 DC_Frankfurt B3_Paris
  5. 3 000002 Plant1 DC_Frankfurt BEAG_Toronto
英文:

We could do this with pivot_wider and fill

  1. library(dplyr)
  2. library(tidyr)
  3. df %&gt;%
  4. pivot_wider(names_from = &#39;step&#39;, values_from = &#39;site&#39;) %&gt;%
  5. fill(starts_with(&#39;step&#39;), .direction = &#39;downup&#39;) %&gt;%
  6. filter(step2 != end_point) %&gt;%
  7. relocate(end_point, .after = last_col())

-output

  1. # A tibble: 3 &#215; 4
  2. product_code step1 step2 end_point
  3. &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  4. 1 000001 Plant1 DC_Frankfurt F6_DC_Bordeaux
  5. 2 000001 Plant1 DC_Frankfurt B3_Paris
  6. 3 000002 Plant1 DC_Frankfurt BEAG_Toronto

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

发表评论

匿名网友

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

确定