Bruce
Bruce

Reputation: 81

How to change my data column based on the value of another column

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!

Upvotes: 0

Views: 34

Answers (1)

jrcalabrese
jrcalabrese

Reputation: 2321

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

Upvotes: 0

Related Questions