R: how to convert delimited string with ranges with the actual numbers in the range? [duplicate]












0
















This question already has an answer here:




  • Split data frame string column into multiple columns

    15 answers



  • Expand ranges defined by “from” and “to” columns

    5 answers




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?










share|improve this question















marked as duplicate by G. Grothendieck, Henrik r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 28 '18 at 23:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Related: stackoverflow.com/questions/46841463/…

    – MrFlick
    Nov 28 '18 at 19:02
















0
















This question already has an answer here:




  • Split data frame string column into multiple columns

    15 answers



  • Expand ranges defined by “from” and “to” columns

    5 answers




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?










share|improve this question















marked as duplicate by G. Grothendieck, Henrik r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 28 '18 at 23:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Related: stackoverflow.com/questions/46841463/…

    – MrFlick
    Nov 28 '18 at 19:02














0












0








0









This question already has an answer here:




  • Split data frame string column into multiple columns

    15 answers



  • Expand ranges defined by “from” and “to” columns

    5 answers




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?










share|improve this question

















This question already has an answer here:




  • Split data frame string column into multiple columns

    15 answers



  • Expand ranges defined by “from” and “to” columns

    5 answers




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?





This question already has an answer here:




  • Split data frame string column into multiple columns

    15 answers



  • Expand ranges defined by “from” and “to” columns

    5 answers








r dplyr etl tidyverse






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 19:01









MrFlick

124k11141173




124k11141173










asked Nov 28 '18 at 19:00









Kristy.ZKristy.Z

93




93




marked as duplicate by G. Grothendieck, Henrik r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 28 '18 at 23:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by G. Grothendieck, Henrik r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 28 '18 at 23:04


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Related: stackoverflow.com/questions/46841463/…

    – MrFlick
    Nov 28 '18 at 19:02



















  • Related: stackoverflow.com/questions/46841463/…

    – MrFlick
    Nov 28 '18 at 19:02

















Related: stackoverflow.com/questions/46841463/…

– MrFlick
Nov 28 '18 at 19:02





Related: stackoverflow.com/questions/46841463/…

– MrFlick
Nov 28 '18 at 19:02












1 Answer
1






active

oldest

votes


















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





share|improve this answer


























  • unfortunately shows error "Error in rep(seq_nrow(data), w) : invalid 'times' argument In addition: Warning message: In eval(cols[[col]], .data, parent.frame()) : NAs introduced by coercion". Do you know the reason? @akrun

    – Kristy.Z
    Nov 28 '18 at 19:08













  • @Kristy.Z Try with the updated code

    – akrun
    Nov 28 '18 at 19:21











  • @ akrun thanks that works! :)

    – Kristy.Z
    Nov 28 '18 at 20:42


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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





share|improve this answer


























  • unfortunately shows error "Error in rep(seq_nrow(data), w) : invalid 'times' argument In addition: Warning message: In eval(cols[[col]], .data, parent.frame()) : NAs introduced by coercion". Do you know the reason? @akrun

    – Kristy.Z
    Nov 28 '18 at 19:08













  • @Kristy.Z Try with the updated code

    – akrun
    Nov 28 '18 at 19:21











  • @ akrun thanks that works! :)

    – Kristy.Z
    Nov 28 '18 at 20:42
















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





share|improve this answer


























  • unfortunately shows error "Error in rep(seq_nrow(data), w) : invalid 'times' argument In addition: Warning message: In eval(cols[[col]], .data, parent.frame()) : NAs introduced by coercion". Do you know the reason? @akrun

    – Kristy.Z
    Nov 28 '18 at 19:08













  • @Kristy.Z Try with the updated code

    – akrun
    Nov 28 '18 at 19:21











  • @ akrun thanks that works! :)

    – Kristy.Z
    Nov 28 '18 at 20:42














0












0








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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 28 '18 at 19:20

























answered Nov 28 '18 at 19:02









akrunakrun

417k13206279




417k13206279













  • unfortunately shows error "Error in rep(seq_nrow(data), w) : invalid 'times' argument In addition: Warning message: In eval(cols[[col]], .data, parent.frame()) : NAs introduced by coercion". Do you know the reason? @akrun

    – Kristy.Z
    Nov 28 '18 at 19:08













  • @Kristy.Z Try with the updated code

    – akrun
    Nov 28 '18 at 19:21











  • @ akrun thanks that works! :)

    – Kristy.Z
    Nov 28 '18 at 20:42



















  • unfortunately shows error "Error in rep(seq_nrow(data), w) : invalid 'times' argument In addition: Warning message: In eval(cols[[col]], .data, parent.frame()) : NAs introduced by coercion". Do you know the reason? @akrun

    – Kristy.Z
    Nov 28 '18 at 19:08













  • @Kristy.Z Try with the updated code

    – akrun
    Nov 28 '18 at 19:21











  • @ akrun thanks that works! :)

    – Kristy.Z
    Nov 28 '18 at 20:42

















unfortunately shows error "Error in rep(seq_nrow(data), w) : invalid 'times' argument In addition: Warning message: In eval(cols[[col]], .data, parent.frame()) : NAs introduced by coercion". Do you know the reason? @akrun

– Kristy.Z
Nov 28 '18 at 19:08







unfortunately shows error "Error in rep(seq_nrow(data), w) : invalid 'times' argument In addition: Warning message: In eval(cols[[col]], .data, parent.frame()) : NAs introduced by coercion". Do you know the reason? @akrun

– Kristy.Z
Nov 28 '18 at 19:08















@Kristy.Z Try with the updated code

– akrun
Nov 28 '18 at 19:21





@Kristy.Z Try with the updated code

– akrun
Nov 28 '18 at 19:21













@ akrun thanks that works! :)

– Kristy.Z
Nov 28 '18 at 20:42





@ akrun thanks that works! :)

– Kristy.Z
Nov 28 '18 at 20:42





Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks