2017-06-16 49 views
0

我有一個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

我想通過流派分開 「|」和填充NA的空列。

這是我做過什麼:

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

然而,每個字母后它被分離。

enter image description here

+0

使用'cSplit'從'splitstackshape'包,'CSPLIT(DF, 「流派」, 「|」)'。 –

+0

請包含拆分的代碼。 – neilfws

回答

2

我想你還沒有包括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")) 

結果:

# 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 

編輯:或者像RichScriven在評論中,df %>% tidyr::separate(genres, into = paste0("genre", 1:5))寫道。有關|分離準確,使用sep = "\\|"

+2

或'到= paste0( 「流派」,1:5)' –

+0

@RichScriven更加美好! – RobertMc

+0

@ RobertMc-對不起我的不完整的代碼,我進行了編輯。你提供的代碼將科學和小說分成不同的列,這不是我想要的。我希望「科幻小說」只在「|」的基礎上進行同一列和分隔。 –

0

那麼,這是什麼樣的幫助下,正確書寫正則表達式。

df %>% 
    separate(genres, into = paste0("genre", 1:5), sep = "\\|") 
相關問題