Hey guys,
probably an easier questions for most of you. How can i manage to convert this:
a1 1
a1 2
a2 1
a2 2
a2 3
a3 1
a3 2
to this:
a1 1 2 0
a2 1 2 3
a3 1 2 0
thanks in advance!
Max
Hey guys,
probably an easier questions for most of you. How can i manage to convert this:
a1 1
a1 2
a2 1
a2 2
a2 3
a3 1
a3 2
to this:
a1 1 2 0
a2 1 2 3
a3 1 2 0
thanks in advance!
Max
Hi bro next time please submit a reproducible example.
Try this:
tibble(col = c('a1','a1','a2','a2','a2', 'a3','a3'),
val = c(1,2,1,2,3,1,2)) %>%
group_by(col) %>%
mutate(rank = row_number()) %>%
ungroup() %>%
spread(key = rank, value = val) %>%
mutate_all(., ~replace_na(.x, 0)) %>%
rename('one' =`1`,'two' = `2` , 'three' = `3`)
# A tibble: 3 x 4
col one two three
<chr> <dbl> <dbl> <dbl>
1 a1 1 2 0
2 a2 1 2 3
3 a3 1 2 0
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.