R将文件路径的字符向量拆分为父目录列表?

时间:2013-08-30 15:09:59

标签: regex r

我有一个文件路径的字符向量:

> tail(paths)
[1] "/home/username/data/dir/GCZ98/GCZ98_1998_12_16.asc.gz"
[2] "/home/username/data/dir/GCZ98/GCZ98_1998_12_20.asc.gz"
[3] "/home/username/data/dir/GCZ99/GCZ99_1999_12_21.asc.gz"
[4] "/home/username/data/dir/GCZ99/GCZ99_1999_12_23.asc.gz"
[5] "/home/username/data/dir/GCZ99/GCZ99_1999_12_27.asc.gz"
[6] "/home/username/data/dir/GCZ99/GCZ99_1999_12_28.asc.gz"

我想通过父文件夹名称将其拆分为矢量列表,即:

> tail(desired)
$ "/home/username/data/dir/GCZ98"
[1] "/home/username/data/dir/GCZ98/GCZ98_1998_12_16.asc.gz"
[2] "/home/username/data/dir/GCZ98/GCZ98_1998_12_20.asc.gz"
$ "/home/username/data/dir/GCZ98"
[1] "/home/username/data/dir/GCZ99/GCZ99_1999_12_21.asc.gz"
[2] "/home/username/data/dir/GCZ99/GCZ99_1999_12_23.asc.gz"
[3] "/home/username/data/dir/GCZ99/GCZ99_1999_12_27.asc.gz"
[4] "/home/username/data/dir/GCZ99/GCZ99_1999_12_28.asc.gz"

我尝试使用splitstrsplit几乎没有成功,但我正在努力寻找能够满足我需求的正则表达式。

感谢您的帮助

2 个答案:

答案 0 :(得分:4)

您可以合并splitdirname

path <- c("/home/username/data/dir/GCZ98/GCZ98_1998_12_16.asc.gz",
          "/home/username/data/dir/GCZ98/GCZ98_1998_12_20.asc.gz",
          "/home/username/data/dir/GCZ99/GCZ99_1999_12_21.asc.gz",
          "/home/username/data/dir/GCZ99/GCZ99_1999_12_23.asc.gz",
          "/home/username/data/dir/GCZ99/GCZ99_1999_12_27.asc.gz",
          "/home/username/data/dir/GCZ99/GCZ99_1999_12_28.asc.gz")

## split by basedir
split(path, dirname(path))

# $`/home/username/data/dir/GCZ98`
# [1] "/home/username/data/dir/GCZ98/GCZ98_1998_12_16.asc.gz" "/home/username/data/dir/GCZ98/GCZ98_1998_12_20.asc.gz"
# 
# $`/home/username/data/dir/GCZ99`
# [1] "/home/username/data/dir/GCZ99/GCZ99_1999_12_21.asc.gz" "/home/username/data/dir/GCZ99/GCZ99_1999_12_23.asc.gz" "/home/username/data/dir/GCZ99/GCZ99_1999_12_27.asc.gz"
# [4] "/home/username/data/dir/GCZ99/GCZ99_1999_12_28.asc.gz"

答案 1 :(得分:2)

正则表达式方法:

> split(paths, gsub("(.*)/[^/]+$", "\\1", paths))
$`/home/username/data/dir/GCZ98`
[1] "/home/username/data/dir/GCZ98/GCZ98_1998_12_16.asc.gz"
[2] "/home/username/data/dir/GCZ98/GCZ98_1998_12_20.asc.gz"

$`/home/username/data/dir/GCZ99`
[1] "/home/username/data/dir/GCZ99/GCZ99_1999_12_21.asc.gz"
[2] "/home/username/data/dir/GCZ99/GCZ99_1999_12_23.asc.gz"
[3] "/home/username/data/dir/GCZ99/GCZ99_1999_12_27.asc.gz"
[4] "/home/username/data/dir/GCZ99/GCZ99_1999_12_28.asc.gz"