Note that there are some explanatory texts on larger screens.

plurals
  1. POPutting variables into a list and then getting them back
    primarykey
    data
    text
    <p>I have some variables per year, that I want to save to disk to retrieve later. Example:</p> <pre><code>Yr.data &lt;- list() #Year 1 a &lt;- "Year 1" b &lt;- 72 c &lt;- matrix(1, nrow=2, ncol=2) Yr.data[[1]] &lt;- list(a=a, b=b, c=c) #Year 2 a &lt;- "Year 2" b &lt;- 99 c &lt;- matrix(3, nrow=2, ncol=2) Yr.data[[2]] &lt;- list(a=a, b=b, c=c) save (Yr.data, file="Yr_data.Rda") rm(a,b,c,Yr.data) </code></pre> <p>Then later I want to get these variables back, for just one year (second year in example):</p> <pre><code>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 &lt;- Yr.data[[2]]$c # I know this is a way to do it, but I want it automatically </code></pre> <p>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...</p> <p>Thanks.</p> <p>/Chris</p> <p>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:</p> <pre><code>Yr.data &lt;- list() b &lt;- 17 Yr.data[[1]] &lt;- list(b=b) b &lt;- 42 Yr.data[[2]] &lt;- list(b=b) b &lt;- Yr.data[[1]]$b # b becomes 17 # Or this, in case I need to analyze the second year b &lt;- Yr.data[[2]]$b # b becomes 42 </code></pre> <p>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 &lt;- 34 first year and d &lt;- 43 second year).</p> <p>Update 2: I apologize for not explaining clearly enough. I don't want to waste your time. Allow me to try one last time.</p> <p>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:</p> <pre><code>Year.I.need.to.analyze &lt;- 1 getAllVariablesInList(Yr.data[[Year.I.need.to.analyze ]]) # creates a, b, c result.I.want &lt;- b * c </code></pre> <p>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.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload