-2

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

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.

Community
  • 1
  • 1
armipunk
  • 458
  • 2
  • 13
  • you can use `gsub` – Hack-R Nov 28 '16 at 02:00
  • Could you elaborate? FWIW, newbie here... – armipunk Nov 28 '16 at 02:29
  • you want to replace part of a string. that's what gsub is for. you can do `gsub(".RData","",i)` to replace the part you don't want with nothing. Check out `?gsub` for more examples. – Hack-R Nov 28 '16 at 02:31
  • I am confused, Strip out from what? Please show us something. After you load your RData, the original named objects remain -not name of file with *.RData* extension. – Parfait Nov 28 '16 at 02:38
  • @Parfait If you use a loop to load saved objects A.Rdata, B.Rdata, C.Rdata, you have objects A, B and C in the environment. To work with those objects, (i) in the for loop needs to be modified to read just A, B, or C, dropping the ".Rdata" part. In my case, I also had different paths pointing to different project directories that also needed to be dropped and user Ott ' s answer below shows how. – armipunk Nov 28 '16 at 15:50
  • Still don't quite know *where* you need the .`Rdata` extension removed. I wished you can show an attempted `for` loop but I see your question answered. Good luck on your project! – Parfait Nov 29 '16 at 16:33
  • @Hack-R There is a simpler, more direct answer than using gsub to strip the filepath and file extension - basename in base R in conjuntion with filepath_sans_ext in the Tools package addressed here: http://stackoverflow.com/questions/15073753/regex-return-file-name-remove-path-and-file-extension – armipunk Nov 29 '16 at 23:18
  • @Parfait - I parsed data and saved objects by year, say y2000.Rdata, y2001.Rdata and so on. Now I want to see a trendline for, say, "man-bites-dog" after the financial crisis of 2008. I need to load (i in files) y2008.Rdata thru y2015.Rdata. As I explained earlier - when you load y2008.Rdata into your environment, the object's name is simply y2008. If you try to do anything with (i) it won't work since the first element (i) of 'files' is y2008.Rdata and what I am trying to manipulate is just "y2008" (minus the quotes). Hope that clarifies. – armipunk Nov 30 '16 at 00:13
  • Save all year objects in a list and then manipulate elements within container (and this avoids many objects in global environ). Plus, the loop's *i* is still a string literal and not the object. You would need `get()` to retrieve object from string but this becomes messy fast. – Parfait Nov 30 '16 at 01:21
  • @Parfait - thanks, yes I wound up with get() and assign(), after noquotes() and as.name() didn't work, and you're absolutely right it gets messy faster than I can blink. I will read up on lists and try your suggestion - I expect to be reading files in batches for a while. – armipunk Nov 30 '16 at 16:13

2 Answers2

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

Edit: Explanation. Technically, these are called "regular expressions" (regexps), not "patterns".

  • . any character

  • .* arbitrary number (including 0) of any kind of characters

  • .*/ 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 /

  • ( and ) enclose groups. You can use the groups when replacing as \\1, \\2 etc.

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.

Ott Toomet
  • 1,894
  • 15
  • 25
  • Thanks. I won't pretend to fully understand, yet, what each element in _.*/([^/]+)", "\\1"_ is doing precisely, but I can see the whole is "pattern-matching" multiple patterns, using a series of wildcards. – armipunk Nov 28 '16 at 15:25
  • Thanks for the Explanation edit. Plain English helps immensely and more comprehensible than ?gsub which had me thoroughly confused. – armipunk Nov 28 '16 at 18:56
  • Pl see my note in comments to my original question. i found your edit explanation incredibly useful, but [this](http://stackoverflow.com/questions/15073753/regex-return-file-name-remove-path-and-file-extension) is more... pre-digested! – armipunk Nov 29 '16 at 23:31
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().

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"
aeongrail
  • 1,304
  • 1
  • 13
  • 26
  • This is useful. I can avoid the extra rm(Objs) at clean up from what I have now: '_rdatafiles <- paste0("/Volumes/Data/RData_files/y", 2004:2009, ".RData") Objs <- paste0("y", 2004:2009)_' Still not clear why the downvotes... – armipunk Jul 18 '17 at 23:44