R: how to convert delimited string with ranges with the actual numbers in the range? [duplicate]
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:
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?
r dplyr etl tidyverse
marked as duplicate by G. Grothendieck, Henrik
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.
add a comment |
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:
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?
r dplyr etl tidyverse
marked as duplicate by G. Grothendieck, Henrik
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
add a comment |
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:
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?
r dplyr etl tidyverse
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:
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
r dplyr etl tidyverse
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
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
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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
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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
Related: stackoverflow.com/questions/46841463/…
– MrFlick
Nov 28 '18 at 19:02