0

i am completely new in R. I am trying to save a spatialdataframe and a normal data frame within the same object. when I am applying the code below, it saves the object as a R workspace, is it normal? i mean I wnated to obtain a .rda data instead. What i specifically want to do is to obtain an R data with those two objects. I want the spatialdataframe to keep its spatial charactheristics. Can someone help me?

##import a text table 
mcvfinal<-read.csv("dataCPWithAge.csv",header=TRUE,sep=",",dec=".") 

##reading the shapefile 
library(rgdal) polypc1 <- readOGR(".", "CP3poly_Matchingshp")   

##saving the two frames into the same object 
save(mcvfinal,polypc1,file="polypc.Rdata")
Lucas Fortini
  • 2,420
  • 15
  • 26
Peter
  • 3
  • 3
  • did the answer work out for you? – harkmug May 31 '13 at 19:41
  • It worked out fine, I am just unable to view the content of the polypc.rds file. When i am doing he str(polypc), it cause an error to the system. it begins opening but nerver ends. – fine, – Peter Jun 02 '13 at 16:12

1 Answers1

4

Try:

saveRDS(list(mcvfinal,polypc1),file="polypc.rds")

Load:

foo = readRDS("polypc.rds")

# mcvfinal is foo[[1]]
# polypc1 is foo[[2]]
harkmug
  • 2,725
  • 22
  • 26
  • I tried it, but it is returning an error:the file was created but it cant be loaded. Pleas can you tell me the difference between .rda and .RDS exensions? Thanks again – Peter Jun 01 '13 at 00:59
  • Sorry i did not read the correct file. The file was succesfully loaded. – Peter Jun 01 '13 at 01:17
  • I am just unable to view the content of the polypc.rds file. When i am doing he str(polypc), it cause an error to the system. it begins opening but nerver ends. – Peter Jun 02 '13 at 16:10
  • Since the command was for saving 2 objects as a list, when you read the `polypc.rds` file, recall that `polypc[[1]]` would be the `mcvfinal` object and `polypc[[2]]` would be the `polypc` object. So try doing `str(polypc[[2]])`. And please *accept* the answer if it worked for you. Thanks! – harkmug Jun 04 '13 at 13:49
  • Hi rmk,thanks a lot for your help. Sorry I am new in this forum and I do not know the rules. Finally it was a problem with the level of memory. Ot worked out perfectly. – Peter Jun 12 '13 at 18:46