Extract first word

前端 未结 3 617
误落风尘
误落风尘 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 16:03

    This should do it.

    dat <- data.frame(Brand = c('Channel clothes',
                               'Gucci perfume',
                               'Channel shoes',
                               'LV purses',
                               'LV scarves'))
    brand <- sub('(^\\w+)\\s.+','\\1',dat$Brand)
    #[1] "Channel" "Gucci"   "Channel" "LV"      "LV" 
    

提交回复
热议问题