I want to render my data table like this. enter image description here I have tried to do it with tidyr or dplyr, but the results are a bit less than ideal. Can anyone help me to do it? Thanks!
Asked
Active
Viewed 27 times
0
-
Welcome to StackOverflow. In any questions, please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with your dataset (or use a built-in dataset like `mtcars`) and any code you've tried out so far, even if it doesn't fully work. – jrcalabrese Feb 04 '23 at 16:51
1 Answers
0
You can use the uncount()
function to "lengthen" your data according to count
.
library(tidyr)
id <- c("A","A","B")
code <- c("S01", "A12", "B11")
count <- c(1,2,2)
df <- data.frame(id, code, count)
df %>%
uncount(count)
id code
1 A S01
2 A A12
3 A A12
4 B B11
5 B B11

jrcalabrese
- 2,184
- 3
- 10
- 30