I wish to spread a table. A picture is attached. I am trying to do so with spread using Tidyverse.
My attempt was:
Want = Have |> spread(key = Group, value = Number)
The result was poor, it kept me with 2 rows per time. Replacing the key or trying multiple values didn't work or ended with error. It has to be simple, how can it be done ?
library(tidyverse)
df <- tribble(
~Time, ~Group, ~Name, ~Number,
1, "A", "AKSJ", 12,
1, "B", "ASJ", 13,
2, "A", "ISJKS", 12,
2, "B", "EFJ", 45,
3, "A", "IF", 12,
3, "B", "DE", 54
)
df |>
pivot_wider(
names_from = Group,
values_from = c(Name, Number),
names_vary = "slowest"
)
#> # A tibble: 3 × 5
#> Time Name_A Number_A Name_B Number_B
#> <dbl> <chr> <dbl> <chr> <dbl>
#> 1 1 AKSJ 12 ASJ 13
#> 2 2 ISJKS 12 EFJ 45
#> 3 3 IF 12 DE 54
Created on 2023-03-28 with reprex v2.0.2