在dplyr中的条件左连接

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

Conditional Left Joining in dplyr

问题

dat1 需要与 dat2 进行左连接,连接条件是 ID,并且选择 dat2 中具有最小 VehPert 值的行。最终的数据将如下所示:

  1. ID Per Gu Ta Veh Pert Ti ID1
  2. 1 1123 112301 14 13 1 1 100 11231
  3. 2 1124 112401 14 19 1 1 107 11241
  4. 3 1125 112501 29 25 2 2 118 11251
  5. 4 1126 112601 22 20 3 1 112 11268

dat2 的第8行中,Per 的最小值为 1,但 Veh4,所以选择了 Veh 为最小值。

英文:

I have two datasets.

Dataset 1 looks like the following:

  1. dat1 <- read.table(header=TRUE, text="
  2. ID Per Gu Ta
  3. 1123 112301 14 13
  4. 1124 112401 14 19
  5. 1125 112501 29 25
  6. 1126 112601 22 20
  7. ")
  8. dat1
  9. ID Per Gu Ta
  10. 1 1123 112301 14 13
  11. 2 1124 112401 14 19
  12. 3 1125 112501 29 25
  13. 4 1126 112601 22 20

Dataset 2 looks like the following:

  1. dat2 <- read.table(header=TRUE, text="
  2. ID Veh Pert Ti ID1
  3. 1123 1 1 100 11231
  4. 1123 2 1 110 11232
  5. 1124 1 1 107 11241
  6. 1124 2 1 111 11242
  7. 1124 3 2 109 11243
  8. 1125 2 2 118 11251
  9. 1125 3 3 113 11252
  10. 1125 4 1 108 11253
  11. 1126 3 4 119 11265
  12. 1126 3 1 112 11268
  13. ")
  14. dat2
  15. ID Veh Pert Ti ID1
  16. 1 1123 1 1 100 11231
  17. 2 1123 2 1 110 11232
  18. 3 1124 1 1 107 11241
  19. 4 1124 2 1 111 11242
  20. 5 1124 3 2 109 11243
  21. 6 1125 2 2 118 11251
  22. 7 1125 3 2 113 11252
  23. 8 1125 4 1 108 11253
  24. 9 1126 3 4 119 11265
  25. 10 1126 3 1 112 11268

dat1 is needed to be left joined with dat2 by ID with rows having the minimum of at first Veh and then Pert of dat2. The final data will be like the following:

  1. ID Per Gu Ta Veh Pert Ti ID1
  2. 1123 112301 14 13 1 1 100 11231
  3. 1124 112401 14 19 1 1 107 11241
  4. 1125 112501 29 25 2 2 118 11251 ### in `row 8` of `dat2` the min value of `Per` is `1` but `Veh` is `4`;
  5. 1126 112601 22 20 3 1 112 11268

答案1

得分: 1

我是你的中文翻译,以下是已翻译好的代码部分:

  1. # 我是你的中文翻译,以下是已翻译好的代码部分:
  2. # 步骤1:使用left_join然后根据Veh和Pert进行arrange排序
  3. # 步骤2:按ID分组并选择第一个值
  4. # 加载dplyr库
  5. library(dplyr)
  6. # 创建dat1数据框
  7. dat1 <- read.table(
  8. header = TRUE,
  9. text = "
  10. ID Per Gu Ta
  11. 1123 112301 14 13
  12. 1124 112401 14 19
  13. 1125 112501 29 25
  14. 1126 112601 22 20
  15. "
  16. )
  17. # 创建dat2数据框
  18. dat2 <- read.table(
  19. header = TRUE,
  20. text = "
  21. ID Veh Pert Ti ID1
  22. 1123 1 1 100 11231
  23. 1123 2 1 110 11232
  24. 1124 1 1 107 11241
  25. 1124 2 1 111 11242
  26. 1124 3 2 109 11243
  27. 1125 2 2 118 11251
  28. 1125 3 3 113 11252
  29. 1125 4 1 108 11253
  30. 1126 3 4 119 11265
  31. 1126 3 1 112 11268
  32. "
  33. )
  34. # 左连接或按Veh然后Pert排序
  35. left_joined_df <-
  36. dat1 %>% left_join(dat2, by = c("ID" = "ID")) %>% arrange(Veh, Pert)
  37. # 按ID分组并选择第一个值
  38. result_df <-
  39. left_joined_df %>% group_by(ID) %>% summarize(
  40. .groups = 'keep',
  41. Per = first(Per),
  42. Gu = first(Gu),
  43. Ta = first(Ta),
  44. Veh = first(Veh),
  45. Pert = first(Pert),
  46. Ti = first(Ti),
  47. ID1 = first(ID1)
  48. ) %>% ungroup()

这是你提供的R代码的翻译版本,仅包括代码部分,没有其他内容。

英文:

I've done it this way:

  1. left_join then arrange based on Veh and Pert
  2. group_by ID and picking first values
  1. library(dplyr)
  2. dat1 &lt;- read.table(
  3. header = TRUE,
  4. text = &quot;
  5. ID Per Gu Ta
  6. 1123 112301 14 13
  7. 1124 112401 14 19
  8. 1125 112501 29 25
  9. 1126 112601 22 20
  10. &quot;
  11. )
  12. dat2 &lt;- read.table(
  13. header = TRUE,
  14. text = &quot;
  15. ID Veh Pert Ti ID1
  16. 1123 1 1 100 11231
  17. 1123 2 1 110 11232
  18. 1124 1 1 107 11241
  19. 1124 2 1 111 11242
  20. 1124 3 2 109 11243
  21. 1125 2 2 118 11251
  22. 1125 3 3 113 11252
  23. 1125 4 1 108 11253
  24. 1126 3 4 119 11265
  25. 1126 3 1 112 11268
  26. &quot;
  27. )
  28. # left join or order by Veh then Pert
  29. left_joined_df &lt;-
  30. dat1 %&gt;% left_join(dat2, by = c(&quot;ID&quot; = &quot;ID&quot;)) %&gt;% arrange(Veh, Pert)
  31. # group by ID and picking first values
  32. result_df &lt;-
  33. left_joined_df %&gt;% group_by(ID) %&gt;% summarize(
  34. .groups = &#39;keep&#39;,
  35. Per = first(Per),
  36. Gu = first(Gu),
  37. Ta = first(Ta),
  38. Veh = first(Veh),
  39. Pert = first(Pert),
  40. Ti = first(Ti),
  41. ID1 = first(ID1)
  42. ) %&gt;% ungroup()

答案2

得分: 1

dat1 |>
left_join(dat2 |>
arrange(Veh, Pert) |>
slice(1, .by = ID))

连接中 by = join_by(ID) 的部分:
ID Per Gu Ta Veh Pert Ti ID1
1 1123 112301 14 13 1 1 100 11231
2 1124 112401 14 19 1 1 107 11241
3 1125 112501 29 25 2 2 118 11251
4 1126 112601 22 20 3 1 112 11268

英文:
  1. dat1 |&gt;
  2. left_join(dat2 |&gt;
  3. arrange(Veh, Pert) |&gt;
  4. slice(1, .by = ID))
  5. Joining with `by = join_by(ID)`
  6. ID Per Gu Ta Veh Pert Ti ID1
  7. 1 1123 112301 14 13 1 1 100 11231
  8. 2 1124 112401 14 19 1 1 107 11241
  9. 3 1125 112501 29 25 2 2 118 11251
  10. 4 1126 112601 22 20 3 1 112 11268

答案3

得分: 1

以下是翻译好的部分:

  1. 容易完成。
  2. library(tidyverse)
  3. dat_join <- left_join(dat1, dat2, by = "ID") %>%
  4. arrange(Veh) %>%
  5. arrange(Pert)
  6. dat_join
英文:

Easily done.

  1. library(tidyverse)
  2. dat_join &lt;- left_join(dat1, dat2, by = &quot;ID&quot;) %&gt;%
  3. arrange(Veh) %&gt;%
  4. arrange(Pert)
  5. dat_join

huangapple
  • 本文由 发表于 2023年6月26日 06:43:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552685.html
匿名

发表评论

匿名网友

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

确定