英文:
How to create a route in a dataframe with 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)
这是我希望它看起来的样子:
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.
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 :
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_wider
和 fill
完成这个操作。
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 %>%
pivot_wider(names_from = 'step', values_from = 'site') %>%
fill(starts_with('step'), .direction = 'downup') %>%
filter(step2 != end_point) %>%
relocate(end_point, .after = last_col())
-output
# A tibble: 3 × 4
product_code step1 step2 end_point
<chr> <chr> <chr> <chr>
1 000001 Plant1 DC_Frankfurt F6_DC_Bordeaux
2 000001 Plant1 DC_Frankfurt B3_Paris
3 000002 Plant1 DC_Frankfurt BEAG_Toronto
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论