1

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

> 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"

我已经尝试过使用split并且strsplit几乎没有成功,但我正在努力尝试找到一个满足我需求的正则表达式。

谢谢你的帮助

4

2 回答 2

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"
于 2013-08-30T15:18:01.797 回答
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"
于 2013-08-30T15:19:25.243 回答