作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在R中,使用file.path
,可以使用适合您平台的正确文件分隔符自动将字符向量转换为完整文件路径:
> file.path("home", "foo", "script.R")
[1] "home/foo/script.R"
split_path <- function(file) {
if(!(file %in% c("/", "."))) {
res <- c(basename(file), split_path(dirname(file)))
return(res)
}
else return(NULL)
}
> split_path("/home/foo/stats/index.html")
[1] "index.html" "stats" "foo" "home"
split_path <- function(path) {
if (dirname(path) %in% c(".", path)) return(basename(path))
return(c(basename(path), split_path(dirname(path))))
}
最佳答案
您可以通过简单的递归函数来做到这一点,方法是终止dirname
不变的时间:
split_path <- function(x) if (dirname(x)==x) x else c(basename(x),split_path(dirname(x)))
split_path("/home/foo/stats/index.html")
[1] "index.html" "stats" "foo" "home" "/"
split_path("C:\\Windows\\System32")
[1] "System32" "Windows" "C:/"
split_path("~")
[1] "James" "Users" "C:/"
关于r - 将文件路径拆分为文件夹名称向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29214932/
我是一名优秀的程序员,十分优秀!