简体   繁体   中英

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 :

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     

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM