字符串提取str_extract与r脚本

发布于 2025-01-25 17:20:27 字数 581 浏览 3 评论 0原文

我有具有此结构的文件:xxx_xxx_xxx_xxx_xxx_class0.pngxxx_xxx_xxx_xxx_xxx_class1.png。我只想选择class0或class1图像。

我做到了:

 ## List images in path
images_names <- list.files("/img/train",pattern="\\.png$",recursive = TRUE)
if(labelsExist){
    ## Select only class0 or class1 images
    classLb <- str_extract(images_names, "^(class0|class1)")
    # Set class0 == 0 and class1 == 1
    key <- c("class0" = 0, "class1" = 1)
    y <- key[classLb]
  }

当我执行时,head(classLB),我在输出中只有na。有什么建议吗?

I have files which have this structure: xxx_xxx_xxx_xxx_class0.png and xxx_xxx_xxx_xxx_class1.png. I want to select only class0 or class1 images.

I did:

 ## List images in path
images_names <- list.files("/img/train",pattern="\\.png
quot;,recursive = TRUE)
if(labelsExist){
    ## Select only class0 or class1 images
    classLb <- str_extract(images_names, "^(class0|class1)")
    # Set class0 == 0 and class1 == 1
    key <- c("class0" = 0, "class1" = 1)
    y <- key[classLb]
  }

When I perform, head(classLb), I have only NA in output. Any suggestions ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有你我更好 2025-02-01 17:20:27

使用sub和REGEX模式。*([az]+)。png。这声称要删除所有内容,直到_遵循一个单词然后.png

sub('.*_([a-z]+).png', '\\1', image_names)

use sub and a regex pattern .*([a-z]+).png. This claims that to delete everything until a _ followed a word then .png

sub('.*_([a-z]+).png', '\\1', image_names)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文