0

I have a dataset looks like this:

enter image description here

So in the second row of the column'leaf_grade', we could see the value of '1-5', so in what I want is the row replicate 5 times with all the values on the row being the same except the 'leaf-grade' changes from 1 to 5.

So the output should look like:

Column 32    33    34       35    36    37    38     colour_grade  leaf_grade
       -775  -700  -625.00  -600  -600  -600  -600   61            1
       -775  -700  -625.00  -600  -600  -600  -600   61            2
       -775  -700  -625.00  -600  -600  -600  -600   61            3
       -775  -700  -625.00  -600  -600  -600  -600   61            4
       -775  -700  -625.00  -600  -600  -600  -600   61            5

Could we achieve this result in r? could we use dplyr to achieve this?

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Kristy.Z
  • 27
  • 3
  • Related: https://stackoverflow.com/questions/46841463/expand-a-data-frame-to-have-as-many-rows-as-range-of-two-columns-in-original-row – MrFlick Nov 28 '18 at 19:02

1 Answers1

0

We can split the 'leaf_grade' at - (assuming that it is a character class column), convert it to numeric, get the sequence with reduce and unnest

library(tidyverse)
df1 %>% 
  mutate(leaf_grade = strsplit(leaf_grade, '-') %>%
                          map( ~ as.numeric(.x) %>%
                                 reduce(`:`))) %>% 
  unnest
akrun
  • 874,273
  • 37
  • 540
  • 662