dplyr: repeating rows based on splitted factor
up vote
0
down vote
favorite
I was wondering if it is possible to use dplyr to repeat rows based on the result of a function.
If I have a data frame that looks like this:
df <- data.frame(count = 1:4, type = LETTERS[1:4], subtype = letters[1:4],
stringsAsFactors = F)
df[2,"subtype"] <- "a,b"
count type subtype
1 1 A a
2 2 B a,b
3 3 C c
4 4 D d
Is it possible to repeat the same row for each subtype letter after splitting them according the the comma? I would like to get something like this:
count type subtype
1 1 A a
2 2 B a
3 2 B b
4 3 C c
5 4 D d
I tried using rowwise
+ do
but I'm still struggling with no results!
Thanks in advance,
Giovanni
r dplyr duplicates rows rowwise
add a comment |
up vote
0
down vote
favorite
I was wondering if it is possible to use dplyr to repeat rows based on the result of a function.
If I have a data frame that looks like this:
df <- data.frame(count = 1:4, type = LETTERS[1:4], subtype = letters[1:4],
stringsAsFactors = F)
df[2,"subtype"] <- "a,b"
count type subtype
1 1 A a
2 2 B a,b
3 3 C c
4 4 D d
Is it possible to repeat the same row for each subtype letter after splitting them according the the comma? I would like to get something like this:
count type subtype
1 1 A a
2 2 B a
3 2 B b
4 3 C c
5 4 D d
I tried using rowwise
+ do
but I'm still struggling with no results!
Thanks in advance,
Giovanni
r dplyr duplicates rows rowwise
Tryseparate_rows
function fromtidyr
: rdrr.io/cran/tidyr/man/separate_rows.html
– AntoniosK
Nov 8 at 12:00
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I was wondering if it is possible to use dplyr to repeat rows based on the result of a function.
If I have a data frame that looks like this:
df <- data.frame(count = 1:4, type = LETTERS[1:4], subtype = letters[1:4],
stringsAsFactors = F)
df[2,"subtype"] <- "a,b"
count type subtype
1 1 A a
2 2 B a,b
3 3 C c
4 4 D d
Is it possible to repeat the same row for each subtype letter after splitting them according the the comma? I would like to get something like this:
count type subtype
1 1 A a
2 2 B a
3 2 B b
4 3 C c
5 4 D d
I tried using rowwise
+ do
but I'm still struggling with no results!
Thanks in advance,
Giovanni
r dplyr duplicates rows rowwise
I was wondering if it is possible to use dplyr to repeat rows based on the result of a function.
If I have a data frame that looks like this:
df <- data.frame(count = 1:4, type = LETTERS[1:4], subtype = letters[1:4],
stringsAsFactors = F)
df[2,"subtype"] <- "a,b"
count type subtype
1 1 A a
2 2 B a,b
3 3 C c
4 4 D d
Is it possible to repeat the same row for each subtype letter after splitting them according the the comma? I would like to get something like this:
count type subtype
1 1 A a
2 2 B a
3 2 B b
4 3 C c
5 4 D d
I tried using rowwise
+ do
but I'm still struggling with no results!
Thanks in advance,
Giovanni
r dplyr duplicates rows rowwise
r dplyr duplicates rows rowwise
asked Nov 8 at 11:49
Giovanni
6117
6117
Tryseparate_rows
function fromtidyr
: rdrr.io/cran/tidyr/man/separate_rows.html
– AntoniosK
Nov 8 at 12:00
add a comment |
Tryseparate_rows
function fromtidyr
: rdrr.io/cran/tidyr/man/separate_rows.html
– AntoniosK
Nov 8 at 12:00
Try
separate_rows
function from tidyr
: rdrr.io/cran/tidyr/man/separate_rows.html– AntoniosK
Nov 8 at 12:00
Try
separate_rows
function from tidyr
: rdrr.io/cran/tidyr/man/separate_rows.html– AntoniosK
Nov 8 at 12:00
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
df <- data.frame(count = 1:4, type = LETTERS[1:4], subtype = letters[1:4],
stringsAsFactors = F)
df[2,"subtype"] <- "a,b"
library(tidyverse)
df %>% separate_rows(subtype)
# count type subtype
# 1 1 A a
# 2 2 B a
# 3 2 B b
# 4 3 C c
# 5 4 D d
1
Brilliant! It worked, thanks a lot!
– Giovanni
Nov 8 at 12:57
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
df <- data.frame(count = 1:4, type = LETTERS[1:4], subtype = letters[1:4],
stringsAsFactors = F)
df[2,"subtype"] <- "a,b"
library(tidyverse)
df %>% separate_rows(subtype)
# count type subtype
# 1 1 A a
# 2 2 B a
# 3 2 B b
# 4 3 C c
# 5 4 D d
1
Brilliant! It worked, thanks a lot!
– Giovanni
Nov 8 at 12:57
add a comment |
up vote
1
down vote
accepted
df <- data.frame(count = 1:4, type = LETTERS[1:4], subtype = letters[1:4],
stringsAsFactors = F)
df[2,"subtype"] <- "a,b"
library(tidyverse)
df %>% separate_rows(subtype)
# count type subtype
# 1 1 A a
# 2 2 B a
# 3 2 B b
# 4 3 C c
# 5 4 D d
1
Brilliant! It worked, thanks a lot!
– Giovanni
Nov 8 at 12:57
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
df <- data.frame(count = 1:4, type = LETTERS[1:4], subtype = letters[1:4],
stringsAsFactors = F)
df[2,"subtype"] <- "a,b"
library(tidyverse)
df %>% separate_rows(subtype)
# count type subtype
# 1 1 A a
# 2 2 B a
# 3 2 B b
# 4 3 C c
# 5 4 D d
df <- data.frame(count = 1:4, type = LETTERS[1:4], subtype = letters[1:4],
stringsAsFactors = F)
df[2,"subtype"] <- "a,b"
library(tidyverse)
df %>% separate_rows(subtype)
# count type subtype
# 1 1 A a
# 2 2 B a
# 3 2 B b
# 4 3 C c
# 5 4 D d
answered Nov 8 at 12:03
AntoniosK
12k1822
12k1822
1
Brilliant! It worked, thanks a lot!
– Giovanni
Nov 8 at 12:57
add a comment |
1
Brilliant! It worked, thanks a lot!
– Giovanni
Nov 8 at 12:57
1
1
Brilliant! It worked, thanks a lot!
– Giovanni
Nov 8 at 12:57
Brilliant! It worked, thanks a lot!
– Giovanni
Nov 8 at 12:57
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53207154%2fdplyr-repeating-rows-based-on-splitted-factor%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try
separate_rows
function fromtidyr
: rdrr.io/cran/tidyr/man/separate_rows.html– AntoniosK
Nov 8 at 12:00