4

I have a matrix:

mat<-matrix(data=1:30,ncol=10,nrow=3)

I would like to save this to Rdata:

save(mat, file="m.Rdata")

Then load it back:

m<-load("m.Rdata")

Then look at its contents:

m
[1] "mat"

All it displays is the name of the matrix saved but the values are lost.

What am I doing wrong?

Also

Once I have saved the matrix I would like to create a new matrix:

mat2<-matrix(data=30:59,ncol=10,nrow=3)

I would then like to save this mat2 into the same Rdata file, what is the right procedure?

user1723765
  • 6,179
  • 18
  • 57
  • 85

2 Answers2

6

Just use load("m.Rdata")

And try ls(). You should see your matrix name.

Consider reading on basics of R - The Workspace

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • excellent thanks! Because I have another very similar question please see my edit. – user1723765 Aug 30 '13 at 09:51
  • Use `save.image()` instead of `save()`. `save.image()` will save all your objects(mat,mat2,etc.) to `.Rdata` at current working directory. – zx8754 Aug 30 '13 at 09:57
0

use

write.table(mat, file="m.Rdata")
m<-read.table("m.Rdata") 
  • Hi @Magdalena, the OP is actually asking about saving the objects. It's a bit specific to R. https://bookdown.org/ndphillips/YaRrr/rdata-files.html. – StupidWolf Jun 20 '20 at 22:20
  • @StupidWolf you have right, I didn't notice the second question, because I was looking for recipe to save and load only one object ;) and when I dig deeper I see that load("m.Rdata") works, but I cannot try to assign result to any vairable – Magdalena Jun 21 '20 at 17:55