0

I have a dataframe with duplicates, and I would like to combine these duplicate pairs in a single line, as follows:

Original dataframe: my_original_dataframe

id.  name. age.  sex.  weigt. date. 
01   ANA    18    F    56.7   02/27/2020 
02   ANA    18    F    59.5   05/05/2020
03   KARLA  21    F    60.1   08/04/2020
04   KARLA  21    F    60.4   12/30/2020
05   MARIA  30    F    49.9   01/06/2020
06   MARIA  30    F    50.2.  03/07/2020

What would I like to do with R: my_desired_dataframe

id.  name   age  sex   weigt1 date1       weight2   date2
01   ANA    18    F    56.7   02/27/2020  59.5      05/05/2020
02   KARLA  21    F    60.1   08/04/2020  60.4      12/30/2020
03   MARIA  30    F    49.9   01/06/2020  50.2      03/07/2020

I will be very grateful for any help!

Jon Spring
  • 55,165
  • 4
  • 35
  • 53

1 Answers1

0

Here's an approach with tidyr::pivot_wider. Note, the current implementation doesn't naturally in the order you're expecting; you can either change it manually, use one of the strategies here, or using a custom pivot_wider_spec().

library(tidyverse)
df1 %>%
  group_by(name) %>% mutate(instance = row_number()) %>% ungroup() %>%
  select(-id) %>%
  pivot_wider(names_from = instance, values_from = c(weight, date), 
              names_glue = "{instance}_{.value}",
              names_sort = TRUE)


# A tibble: 3 x 7
  name    age sex   `1_weight` `2_weight` `1_date`   `2_date`  
  <chr> <int> <lgl>      <dbl>      <dbl> <chr>      <chr>     
1 ANA      18 FALSE       56.7       59.5 02/27/2020 05/05/2020
2 KARLA    21 FALSE       60.1       60.4 08/04/2020 12/30/2020
3 MARIA    30 FALSE       49.9       50.2 01/06/2020 03/07/2020
Jon Spring
  • 55,165
  • 4
  • 35
  • 53