Reputation: 121
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
Upvotes: 0
Views: 58
Reputation: 16121
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
Upvotes: 1