0

I have a tibble.

library(tidyverse)
df <- tibble(
  id = 1:4,
  genres = c("Action|Adventure|Science Fiction|Thriller", 
        "Adventure|Science Fiction|Thriller",
        "Action|Crime|Thriller",
        "Family|Animation|Adventure|Comedy|Action")
)
df

enter image description here

I want to separate the genres by "|" and empty columns filled with NA.

This is what I did:

df %>% 
  separate(genres, into = c("genre1", "genre2", "genre3", "genre4", "genre5"), sep = "|")

However, it's being separated after each letter.

enter image description here

arjan-hada
  • 251
  • 3
  • 17

2 Answers2

2

I think you haven't included into:

df <- tibble::tibble(
  id = 1:4,
  genres = c("Action|Adventure|Science Fiction|Thriller", 
             "Adventure|Science Fiction|Thriller",
             "Action|Crime|Thriller",
             "Family|Animation|Adventure|Comedy|Action")
)
df %>% tidyr::separate(genres, into = c("genre1", "genre2", "genre3", 
                 "genre4", "genre5"))

Result:

# A tibble: 4 x 6
     id    genre1    genre2    genre3   genre4   genre5
* <int>     <chr>     <chr>     <chr>    <chr>    <chr>
1     1    Action Adventure   Science  Fiction Thriller
2     2 Adventure   Science   Fiction Thriller     <NA>
3     3    Action     Crime  Thriller     <NA>     <NA>
4     4    Family Animation Adventure   Comedy   Action

Edit: Or as RichScriven wrote in the comments, df %>% tidyr::separate(genres, into = paste0("genre", 1:5)). For separating on | exactly, use sep = "\\|".

RobertMyles
  • 2,673
  • 3
  • 30
  • 45
0

Well, this is what helped, writing regex properly.

df %>% 
  separate(genres, into = paste0("genre", 1:5), sep = "\\|")
arjan-hada
  • 251
  • 3
  • 17