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

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

How to create a route in a dataframe with R

问题

以下是翻译好的部分:

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

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

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

end_point <- c("DC_Frankfurt","F6_DC_Bordeaux","B3_Paris","BEAG_Toronto","DC_Frankfurt")
product_code <- c("000001","000001","000001","000002","000002")
site <- c("Plant1","DC_Frankfurt","DC_Frankfurt","DC_Frankfurt","Plant2")
step <- c("step1","step2","step2","step2","step1")

df <- data.frame(end_point, product_code,site,step)

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

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

product_code <- c("000001","000001","000002")
step1 <- c("Plant1","Plant1","Plant2")
step2 <- c("DC_Frankfurt","DC_Frankfurt","DC_Frankfurt")
end_point <- c("F6_DC_Bordeaux","B3_Paris","BEAG_Toronto")

result_expected <- data.frame(product_code,step1,step2,end_point)

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

my_df_test <- df %>%
  pivot_wider(names_from = step,
              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中创建数据框的路由

end_point <- c("DC_Frankfurt","F6_DC_Bordeaux","B3_Paris","BEAG_Toronto","DC_Frankfurt")
product_code <- c("000001","000001","000001","000002","000002")
site <- c("Plant1","DC_Frankfurt","DC_Frankfurt","DC_Frankfurt","Plant2")
step <- c("step1","step2","step2","step2","step1")

df <- data.frame(end_point, product_code,site,step)

Here is what I would like it to be :

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

product_code <- c("000001","000001","000002")
step1 <- c("Plant1","Plant1","Plant2")
step2 <- c("DC_Frankfurt","DC_Frankfurt","DC_Frankfurt")
end_point <- c("F6_DC_Bordeaux","B3_Paris","BEAG_Toronto")

result_expected <- data.frame(product_code,step1,step2,end_point)

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

my_df_test <- df %>%
  pivot_wider(names_from = step,
              values_from = site)

Thank you all!

答案1

得分: 1

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

library(tidyverse)

df %>%
  filter(step == "step2") %>%
  rename(step2 = site) %>%
  select(-step) %>%
  left_join(df %>%
              filter(step == "step1") %>%
              rename(step1 = site) %>%
              select(-step, -end_point), by = "product_code") %>%
  select(product_code, step1, step2, end_point)
#>   product_code  step1        step2      end_point
#> 1       000001 Plant1 DC_Frankfurt F6_DC_Bordeaux
#> 2       000001 Plant1 DC_Frankfurt       B3_Paris
#> 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:

library(tidyverse)

df %>%
  filter(step == "step2") %>%
  rename(step2 = site) %>%
  select(-step) %>%
  left_join(df %>% 
              filter(step == "step1") %>%
              rename(step1 = site) %>%
              select(-step, -end_point), by = "product_code") %>%
  select(product_code, step1, step2, end_point)
#>   product_code  step1        step2      end_point
#> 1       000001 Plant1 DC_Frankfurt F6_DC_Bordeaux
#> 2       000001 Plant1 DC_Frankfurt       B3_Paris
#> 3       000002 Plant2 DC_Frankfurt   BEAG_Toronto

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

答案2

得分: 1

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

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

-输出

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

We could do this with pivot_wider and fill

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

-output

# A tibble: 3 &#215; 4
  product_code step1  step2        end_point     
  &lt;chr&gt;        &lt;chr&gt;  &lt;chr&gt;        &lt;chr&gt;         
1 000001       Plant1 DC_Frankfurt F6_DC_Bordeaux
2 000001       Plant1 DC_Frankfurt B3_Paris      
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:

确定