Really this is a data problem in format .rdata
, because you don’t know what’s inside the file and they can overwrite various things. If you do this manually it can be quite complicated, you will have to read a file, rename all objects, then read the other file and so on.
One solution is not to upload your files to your main desktop. You create a separate environment and upload the files there. Let me give you an example.
Let’s create two files .rda
example containing objects with the same name:
rm(list = ls())
save(mtcars, iris, file = "dados1.rda")
save(mtcars, iris, file = "dados2.rda")
Now suppose you want to read these files but don’t want the objects to be overwritten.
The first thing you have to do is create a new environment (it’s like a list):
dados1 <- new.env()
And now it’s time to give load()
you will speak to give load()
in that environment.
load("dados1.rda", envir = dados1)
If you want to access the data you access as if it were a list:
dados1$iris
dados1$mtcars
Now you can read the dados2.rda
without overwriting the objects of dados1
:
dados2 <- new.env()
load("dados2.rda", envir = dados2)
If you don’t want to work with environments
you can turn them into list:
dados1 <- as.list(dados1)
But if you are going through too much of this problem, it might be a good idea to save the files in format .rds
so you can read with whatever name you want, without having to do all this gymnastics.
Marcus Nunes, thank you for your help. It worked!
– Naomi