Reputation: 11
I have a data set similar to this one:
name tag
Jane [nice:5/7], [not funny:4/4], [strange:5/7], [smart:7/7]
Jack [old:10/10], [very cute:4/6]
Tom [awesome:2/4]
I would like to turn it into the data set that looks like this:
name tag number1 number2
Jane nice 5 7
Jane not funny 4 4
Jane strange 5 7
Jane smart 7 7
Jack old 10 10
Jack very cute 4 6
Tom awesome 2 4
How should I approach it? Maybe there are some nice packages that could help?
Thanks!
Upvotes: 0
Views: 27
Reputation: 24770
Here's an approach with separate_rows
and extract
from tidyr
:
Edit: Now with two word tags.
library(dplyr)
library(tidyr)
data %>%
separate_rows(tag, sep = ", ") %>%
extract(tag, into = c("tag","number1", "number2"),
regex = "\\[(.+):([0-9]+)/([0-9]+)\\]")
# A tibble: 7 x 4
name tag number1 number2
<fct> <chr> <chr> <chr>
1 Jane nice 5 7
2 Jane not funny 4 4
3 Jane strange 5 7
4 Jane smart 7 7
5 Jack old 10 10
6 Jack very cute 4 6
7 Tom awesome 2 4
Upvotes: 1