First entry from string split

后端 未结 5 1978
陌清茗
陌清茗 2020-12-03 04:30

I\'ve got a column people$food that has entries like chocolate or apple-orange-strawberry.

I want to split people$food

5条回答
  •  星月不相逢
    2020-12-03 05:28

    I would suggest using head rather than [ in R.

    word <- c('apple-orange-strawberry','chocolate')
    sapply(strsplit(word, "-"), head, 1)
    # [1] "apple"     "chocolate"
    

提交回复
热议问题