Extract first word

前端 未结 3 615
误落风尘
误落风尘 2021-01-06 15:29

I have the following data frame dat :

brand (column name)
Channel clothes
Gucci perfume
Channel shoes
LV purses
LV scarves

And I want to cr

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-06 15:43

    I prefer the tidyverse approach.

    With this dataset:

    library(tidyverse)
    
    df <- tribble(
      ~brand,
      "Channel clothes",
      "Gucci perfume",
      "Channel shoes",
      "LV purses",
      "LV scarves"
    )
    

    We can separate the column with the following:

    df %>% 
      separate(brand, into = c("brand", "item"), sep = " ")
    

    Which returns:

    # A tibble: 5 x 2
        brand    item
    *      
    1 Channel clothes
    2   Gucci perfume
    3 Channel   shoes
    4      LV  purses
    5      LV scarves
    

提交回复
热议问题