楼主: dxystata
9711 14

[问答] R 中如何得到上一级目录 [推广有奖]

11
RiverSide 发表于 2013-1-3 00:12:04
脱裤子放屁太麻烦 直接用函数dirname即可

12
dxystata 发表于 2013-1-3 10:30:54
qoiqpwqr 发表于 2012-12-16 12:34
这个有点麻烦

如果你原来的的字符串是d:\temp\aaa.jpg,\t和\a会被认为是特殊的字符,比如\t代表tab
传递进去的!

13
pacifly 发表于 2013-1-4 03:23:20
"问题1:如何根据d:\temp\aaa.jpg 得到d:/temp/aaa.jpg  ?"

The path is not in correct form. It should either be "d:\\temp\\aaa.jpg" or "d:/temp/aaa.jpg".
So it is interpreted by R that your path contains "\t" and "\a" rather than a directory slash.

"问题2:如何根据d:/temp/aaa.jpg 得到上一级目录d:/temp/?"

a="d:/temp/aaa.jpg"
regmatches(a, regexpr(".+/",a))


14
dxystata 发表于 2013-1-4 09:31:57
pacifly 发表于 2013-1-4 03:23
"问题1:如何根据d:\temp\aaa.jpg 得到d:/temp/aaa.jpg  ?"
The path is not in correct form. It should either be "d:\\temp\\aaa.jpg" or "d:/temp/aaa.jpg".
So it is interpreted by R that your path contains "\t" and "\a" rather than a directory slash.

"问题2:如何根据d:/temp/aaa.jpg 得到上一级目录d:/temp/?"
a="d:/temp/aaa.jpg"
regmatches(a, regexpr(".+/",a))
如果传递进去就是 d:\temp\aaa.jpg呢?谢谢!

15
pacifly 发表于 2013-1-5 03:48:19
dxystata 发表于 2013-1-4 09:31
如果传递进去就是 d:\temp\aaa.jpg呢?谢谢!
no way to automate this because R can't parse the string correctly in such case. Suppose change your original path to "d:\temp\caa.jpg", this will not be valid in R (you can try print it in R console), because "\c" is not a recognized escape. Your original path was just lucky to go through R because "\t" and "\a" happen to be those recognized ones.

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-31 15:59