Chris
Chris

Reputation: 2256

Putting variables into a list and then getting them back

I have some variables per year, that I want to save to disk to retrieve later. Example:

Yr.data <- list()

#Year 1
a <- "Year 1"
b <- 72
c <- matrix(1, nrow=2, ncol=2)
Yr.data[[1]] <- list(a=a, b=b, c=c)

#Year 2
a <- "Year 2"
b <- 99
c <- matrix(3, nrow=2, ncol=2)
Yr.data[[2]] <- list(a=a, b=b, c=c)

save (Yr.data, file="Yr_data.Rda")

rm(a,b,c,Yr.data)

Then later I want to get these variables back, for just one year (second year in example):

load("Yr_data.Rda")
# Here I want to "unlist" Yr.data[[2]], so I get a, b, c as separate variables
print(b)
[1] 99
c <- Yr.data[[2]]$c # I know this is a way to do it, but I want it automatically

Is there a smart way to do this? Both to save the variables without having to write a=a and so on, and without specifically specifying the variables to get out. The real data I want to save per year is much more complex (spatial objects, dataframes, etc.). I think the solution is simple, but somehow I´m stuck in finding it...

Thanks.

/Chris

Update: Thanks for efforts to help, I really appreciate it. I realize the problem description was not clear enough. For each year (and measurement points) I have for example the variables b and c (but with different values per year and measurement points). These values I need to save to disk for later processing. b can for example be a list() or a SpatialPolygonsDataFrame[] (not sure how to put that into a database). I have other R-scripts to process the variables b and c. The complexity is that I want to save "b" several times in the same file. So I thought it was smart to put it in a list:

Yr.data <- list()  
b <- 17
Yr.data[[1]] <-  list(b=b)
b <- 42
Yr.data[[2]] <- list(b=b) 

b <- Yr.data[[1]]$b # b becomes 17
# Or this, in case I need to analyze the second year
b <- Yr.data[[2]]$b # b becomes 42

This code does it, but I was hoping to learn a more robust way to do it in case I later add more variables (for example d <- 34 first year and d <- 43 second year).

Update 2: I apologize for not explaining clearly enough. I don't want to waste your time. Allow me to try one last time.

I have an R-script that process input variables a, b, c. In my examples these variables are simple, but in reality they are more complex objects like sp::SpatialPolygonsDataFrame so I can't put them in dataframes. Sometimes I need to process one set of variables, sometimes another. I thought it good to save these different sets as lists in a list, so if I want to run with first set, I select the first list in the list of lists:

Year.I.need.to.analyze <- 1
getAllVariablesInList(Yr.data[[Year.I.need.to.analyze ]]) # creates a, b, c
result.I.want <- b * c

And when I need to analyze the second year, I just have to change the "Year.I.need.to.analyze" to 2 and run the script again. I would prefer to not save each set in a separate .Rda-file, to avoid "greping" and "paste():ing" on filenames and directories, and also avoid to keep track of which files are needed and so on.

I know the example is simple, but the real script must hop between years, create and export plots in between, and so on. I was hoping to automize all that in a robust way. Again sorry for the confusion. After hours of trying to fix this, I realize I might be too tired to explain the question in the best way.

Upvotes: 0

Views: 1098

Answers (3)

Chris
Chris

Reputation: 2256

I finally managed to do it. Perhaps it would be better with an apply-function. Thanks again for all support.

Yr.data.x <- Yr.data[[1]]   #  Select year to work with

for (i in 1:length(Yr.data.x)) {
  name.i <- names(Yr.data.x)[i]
  value.i <- Yr.data.x[[i]]
  assign(name.i, value.i)
}
rm(i, name.i, value.i, Yr.data.x)

/Chris

Upvotes: 0

IRTFM
IRTFM

Reputation: 263471

The way to accomplish this is to use a database interface to R. There are several to choose from. Both MySQL and SQLite have well tested interfaces:

Recommendations for database with R

RSQLite would have the added advantage in my mind that it is well integrated with the sqldf package.

Upvotes: 1

shhhhimhuntingrabbits
shhhhimhuntingrabbits

Reputation: 7475

maybe something like

L3 <- LETTERS[1:3]
(d <- data.frame(cbind(x=1, y=1:10), fac=sample(L3, 10, replace=TRUE)))

## The same with automatic column names:
y<-data.frame(cbind(  1,   1:10),     sample(L3, 10, replace=TRUE))

dput(y,file='test.data')
rm(y)
y<-dget(file='test.data')

not sure I get what you are asking for.

or try

dump(c("L3","y"),'test.data')

Upvotes: 0

Related Questions