[英]R. Creating a row from column based on specific conditions
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!谢谢!
Here's an approach with separate_rows
and extract
from tidyr
:这是一种使用
separate_rows
并从tidyr
extract
的方法:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.