gpt4 book ai didi

r - 提取第一个词

转载 作者:行者123 更新时间:2023-12-01 07:51:48 24 4
gpt4 key购买 nike

我有以下数据框 dat :

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

我想创建一个只有品牌名称的新列,所以无论后面是什么,列品牌的第一个词。我想要输出如下:
brand (column name)
Channel
Gucci
Channel
LV
LV

我尝试使用带有以下代码的 sub ,但它不起作用。你能帮我看看我的代码有什么问题吗?
brand <- sub("(\\w+).*", "\\1", dat$brand)

最佳答案

我们可以使用 word来自 stringr

library(stringr)
word(df$brand, 1)
#[1] "Channel" "Gucci" "Channel" "LV" "LV"

关于r - 提取第一个词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46917572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com