0

I'm wanting to transpose multiple values (character and numeric) by an ID variable and am really struggling..

What I have

id = c("A1", "A1", "B1", "B1", "B1")
type = c("a", "b", "c", "d", "e")
first_value = c(1, 2, 3, 4, 5)
second_value = c(6, 7, 8, 9, 10)
have = data.frame(cbind(id, type, first_value, second_value))

What I want

id = c("A1", "B1")
type_1 = c("a", "c")
first_value_1 = c(1, 3)
second_value_1 = c(6, 8)
type_2 = c("d", "f")
first_value_2 = c(2, 4)
second_value_2 = c(7, 9)
type_3 = c(NA, "g")
first_value_3 = c(NA, 5)
second_value_3 = c(NA, 10)
want = data.frame(cbind(id, 
                        type_1, first_value_1, second_value_1,
                        type_2, first_value_2, second_value_2,
                        type_3, first_value_3, second_value_3))

Any help would be much appreciated!

Thanks in advance

Brian
  • 83
  • 7
  • If you create your own sequence value, you can use `dplyr` and `tidyr` with `have %>% group_by(id) %>% mutate(seq=row_number()) %>% pivot_wider(id_cols=id, names_from = seq, values_from=type:second_value, names_vary = "slowest")` – MrFlick May 26 '22 at 14:35

0 Answers0