Hi, I'm stuck with this problem and I don't know how to solve it.

I have this base

id | math | X | history | Y | geo | Z |
---|---|---|---|---|---|---|

1 | 1/12/2023 | A | 9/10/2023 | A | 14/9/2023 | A |

1 | 15/9/2023 | B | 4/7/2023 | D | 19/2/2023 | D |

2 | 18/12/2023 | A | 6/7/2023 | C | 10/4/2023 | A |

2 | 30/8/2023 | C | 5/6/2023 | B | 24/12/2023 | C |

3 | 5/6/2023 | D | 19/3/2023 | A | 27/5/2023 | D |

3 | 11/11/2023 | B | 14/6/2023 | A | 27/9/2023 | B |

and I need each id to have only one observation like this

id | math_1 | math_2 | math_x_1 | math_x_2 | history_1 | history_2 | history_y_1 | history_y_2 | geo_1 | geo_2 | geo_z_1 | geo_z_2 |
---|---|---|---|---|---|---|---|---|---|---|---|---|

1 | 1/12/2023 | 15/9/2023 | A | B | 9/10/2023 | 4/7/2023 | A | D | 14/9/2023 | 19/2/2023 | A | D |

2 | 18/12/2023 | 30/8/2023 | A | C | 6/7/2023 | 5/6/2023 | C | B | 10/4/2023 | 24/12/2023 | A | C |

3 | 5/6/2023 | 11/11/2023 | D | B | 19/3/2023 | 14/6/2023 | A | A | 27/5/2023 | 27/9/2023 | D | B |

How can I condense all the information in a single row? I know I have to use pivot_longer but I don't know how to solve it.