-2

I am a new user of R. I have three RData files with the same object name and I want to merge it so that I will have one .Rdata file with one object name.

Example:

  • file1.RData with object name A
  • file2.RData with object name A
  • file3.RData with object name A

and Result should be

  • file.RData = object A

I tried rbind and merge command, but nothing is working.

demongolem
  • 9,474
  • 36
  • 90
  • 105

2 Answers2

1

You will need to save them as new objects as you load each, then do your merging. For example, here, create a list to hold the objects. Then, as you load each, add that version to your list.

listForFiles <- list()

load("file1.RData")
listForFiles[[1]] <- A

load("file2.RData")
listForFiles[[2]] <- A

load("file3.RData")
listForFiles[[3]] <- A

Then, you can use listForFiles to do your merging. Since you don't say what kind of object these are, I can't suggest an approach.

Mark Peterson
  • 9,370
  • 2
  • 25
  • 48
  • Mark thanks a lot. I will try your method. By the way the class of the object A is List which contains dataframe..I will be happy to receive more suggestions about it. – user3056633 Nov 10 '16 at 14:21
1

This is why you might want to consider saving your files as .RDS format. It is similar to .RDA, but it only saves one object at a time (with the saveRDS() command). To read then, you can use the readRDS() function and assign the object to whatever variable name you want. This is specially useful for large projects where you may have lots of data frames with generic names and eventually want to load them in a common script. It will save some time!

bprallon
  • 41
  • 1
  • 5
  • Yes finally I solved it by suing very simple steps and in future I will save the data in .RDS format to avoid these kind of problem t1 <- get(load("file1.Rdata")) , t2 <- get(load("file2.Rdata")), t3 <- get(load("file3.Rdata")) and then A <- c(t1,t2,t3) ....which is also mentioned by Mark... – user3056633 Nov 10 '16 at 19:08