字符串提取str_extract与r脚本
我有具有此结构的文件:xxx_xxx_xxx_xxx_xxx_class0.png
和xxx_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="\\.pngquot;,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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
sub
和REGEX模式。*([az]+)。png
。这声称要删除所有内容,直到_
遵循一个单词然后.png
use
sub
and a regex pattern.*([a-z]+).png
. This claims that to delete everything until a_
followed a word then.png