【发布时间】:2018-12-19 16:41:56
【问题描述】:
我想使用 R 函数在给定路径 (path) 的父文件夹 (pathPos) 中创建一个新文件夹 (newPack)。
path <- "/m/home/user/unix/R/3.5/stringi"
newPack <- "stringr"
pathPos <- stringi::stri_locate_last_fixed(path, '/')[-1]
pathNew <- paste(stringi::stri_sub(path, 1, pathPos), newPack, sep = '')
dir.create(pathNew)
我可以使用上面的代码实现这一点,但我强烈认为有更好的选择。如果你知道的话,请告诉我。
【问题讨论】:
-
也许是
?basename。 -
我猜你的意思是
dirname()。谢谢,它避免使用stringi函数:stri_locate_last_fixed,stri_sub在上面的 sn-p 中。即pathNew <- paste(dirname(path), '/', newPack, sep = '') -
setwd(path); setwd(".."); dir.create(newPack);怎么样 -
@Prradep 哎呀!对,
dirname。 -
@Mako212 谢谢,我可以使用它,但是三次更改工作目录会更乏味:(