I have several large R objects saved as .RData files: "this.RData", "that.RData", "andTheOther.RData" and so on. I don't have enough memory, so I want to load each in a loop, extract some rows, and unload it. However, once I load(i), I need to strip the ".RData" part of (i) before I can do anything with objects "this", "that", "andTheOther". I want to do the opposite of what is described in How to iterate over file names in a R script? How can I do that? Thx
我有几个大的R对象保存为.RData文件:“this.RData”,“that.RData”,“andTheOther.RData”等等。我没有足够的内存,所以我想在循环中加载每个内容,提取一些行并卸载它。然而,一旦我加载(i),我需要剥离(i)的“.RData”部分,然后才能对“this”,“that”,“andTheOther”这些对象做任何事情。我想做的是如何在R脚本中迭代文件名中描述的内容?我怎样才能做到这一点?谢谢
Edit: I omitted to mention the files are not in the working directory and have a filepath as well. I came across Getting filename without extension in R and file_path_sans_ext takes out the extension but the rest of the path is still there.
编辑:我省略提到文件不在工作目录中并且也有文件路径。我在R中遇到了没有扩展名的获取文件名,而file_path_sans_ext取出了扩展名,但路径的其余部分仍然存在。
2 个解决方案
#1
0
Do you mean something like this?
你的意思是这样的吗?
i <- c("/path/to/this.RDat", "/another/path/to/that.RDat")
f <- gsub(".*/([^/]+)", "\\1", i)
f1 <- gsub("\\.RDat", "", f)
f1
[1] "this" "that"
On windows' paths you have to use "\\"
instead of "/"
在Windows的路径上,您必须使用“\\”而不是“/”
Edit: Explanation. Technically, these are called "regular expressions" (regexps), not "patterns".
编辑:说明。从技术上讲,这些被称为“正则表达式”(regexps),而不是“模式”。
-
.
any character。任何角色
-
.*
arbitrary number (including 0) of any kind of characters。*任意类型的任意数字(包括0)
-
.*/
arbitrary number of any kind of characters, followed by a/
。* /任意类型的任意数字,后跟一个/
-
[^/]
any character but not/
[^ /]任何角色但不是/
-
[^/]+
arbitrary number (1 or more) of any kind of characters, but not/
[^ /] +任意类型的任意数字(1或更多),但不是/
-
(
and)
enclose groups. You can use the groups when replacing as\\1
,\\2
etc.(和)包围组。替换时可以使用组作为\\ 1,\\ 2等。
So, look for any kind of character, followed by /
, followed by anything but not the path separator. Replace this with the "anything but not separator".
所以,寻找任何类型的字符,后跟/,后跟任何东西,但不是路径分隔符。将其替换为“除了分隔符之外的任何东西”。
There are many good tutorials for regexps, just look for it.
regexp有很多很好的教程,只需要寻找它。
#2
0
A simple way to do this using would be to extract the base name from the filepaths with base::basename()
and then remove the file extension with tools::file_path_sans_ext()
.
使用base :: basename()从文件路径中提取基本名称然后使用tools :: file_path_sans_ext()删除文件扩展名的简单方法是使用base :: basename()从文件路径中提取基本名称。
paths_to_files <- c("./path/to/this.RData", "./another/path/to/that.RData")
tools::file_path_sans_ext(
basename(
paths_to_files
)
)
## Returns:
## [1] "this" "that"
#1
0
Do you mean something like this?
你的意思是这样的吗?
i <- c("/path/to/this.RDat", "/another/path/to/that.RDat")
f <- gsub(".*/([^/]+)", "\\1", i)
f1 <- gsub("\\.RDat", "", f)
f1
[1] "this" "that"
On windows' paths you have to use "\\"
instead of "/"
在Windows的路径上,您必须使用“\\”而不是“/”
Edit: Explanation. Technically, these are called "regular expressions" (regexps), not "patterns".
编辑:说明。从技术上讲,这些被称为“正则表达式”(regexps),而不是“模式”。
-
.
any character。任何角色
-
.*
arbitrary number (including 0) of any kind of characters。*任意类型的任意数字(包括0)
-
.*/
arbitrary number of any kind of characters, followed by a/
。* /任意类型的任意数字,后跟一个/
-
[^/]
any character but not/
[^ /]任何角色但不是/
-
[^/]+
arbitrary number (1 or more) of any kind of characters, but not/
[^ /] +任意类型的任意数字(1或更多),但不是/
-
(
and)
enclose groups. You can use the groups when replacing as\\1
,\\2
etc.(和)包围组。替换时可以使用组作为\\ 1,\\ 2等。
So, look for any kind of character, followed by /
, followed by anything but not the path separator. Replace this with the "anything but not separator".
所以,寻找任何类型的字符,后跟/,后跟任何东西,但不是路径分隔符。将其替换为“除了分隔符之外的任何东西”。
There are many good tutorials for regexps, just look for it.
regexp有很多很好的教程,只需要寻找它。
#2
0
A simple way to do this using would be to extract the base name from the filepaths with base::basename()
and then remove the file extension with tools::file_path_sans_ext()
.
使用base :: basename()从文件路径中提取基本名称然后使用tools :: file_path_sans_ext()删除文件扩展名的简单方法是使用base :: basename()从文件路径中提取基本名称。
paths_to_files <- c("./path/to/this.RData", "./another/path/to/that.RData")
tools::file_path_sans_ext(
basename(
paths_to_files
)
)
## Returns:
## [1] "this" "that"