I have this code :
library(tidyverse)
input_data = tribble(
~"id", ~"name", ~"surname", ~"date_test", ~"result_test",
1 , "franck" , "wilson" , "01-2002" , "P",
1 , "franck" , "wilson" , "07-2004" , "N",
1 , "franck" , "wilson" , "04-2008" , "N",
2 , "annie", "mark", "08-2001" , "N",
2 , "annie", "mark", "03-2005" , "P"
)
output_data = input_data %>%
# add a counter for the tests performed
group_by(id, name, surname) %>%
mutate(test_rep = 1:n()) %>%
pivot_longer(cols = c(date_test, result_test),
names_to = "variable",
values_to = "readout") %>%
pivot_wider(id_cols = c(id, name, surname),
names_from = c(variable, test_rep),
names_sep = "_",
values_from = readout)
output_data
# A tibble: 2 x 9
# Groups: id, name, surname [2]
id name surname date_test_1 result_test_1 date_test_2 result_test_2 date_test_3 result_test_3
<dbl> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 1 franck wilson 01-2002 P 07-2004 N 04-2008 N
2 2 annie mark 08-2001 N 03-2005 P NA NA
and I want to recover in a variable the number max of tests, like here 3. I tried unsuccessfully with something like this
maxTest<- n
Thank you for your help