【发布时间】:2017-02-07 05:35:12
【问题描述】:
这是一个简单的问题,但我在下面遗漏了一些东西。
我在 R 中有一个非常大的文件路径向量(即字符串)
vec = c("\dir\subdir\pathname1\file.txt", "\dir\subdir\pathname1\file.pdf",
..., "\dir\subdir\pathname9\file.jpg")
我的想法是为每个“类型”文件创建一个data.table 对象,例如.txt、.pdf 等。因此,我需要通过对上述内容进行过滤来为每个文件扩展名创建一个 R 向量。
搜索带有特定扩展名的字符串的方法是grepl():
grepl(".txt$", vec)
现在,如何使用 grepl() 创建一个新向量?终点应该是
txt_paths <- # single vector only with txt files
pdf_paths <- # single vector only with pdf files
jpg_paths <- # single vector only with jpg files
etc.
【问题讨论】: