Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not sure what you mean by "a function accessing each list individually", but this is pretty straightforward using "lapply" and "do.call('rbind',...)":</p> <p>I couldn't load your .RData file, so this code works for the list:</p> <pre><code>ul &lt;- list(param = list( c(name = "Section", value = "1"), c(name = "field", value = "1"), c(name = "final answer", value = "1"), c(name = "points", value = "-0.0")), param = list( c(name = "Section", value = "1"), c(name = "field", value = "2"), c(name = "final answer", value = "1"), c(name = "points", value = "1.0"))) </code></pre> <p>You may have to tweak the details if your list is different; the general principal will remain the same. Just to keep the code clean, let's define the 'extractitem' function that's going to pull out all of the names or values for ul[[1]], ul[[2]], etc. This function is a little more general than you need.</p> <pre><code>extractitem &lt;- function(listelement, item) unname(lapply(listelement, function(itemblock) itemblock[item])) </code></pre> <p>Now we'll just use lapply to walk through ul element by element; for each element, we extract the values into a data frame, then name the columns according to the 'names'. </p> <pre><code>rowlist &lt;- lapply(ul, function(listelement) { d &lt;- data.frame(extractitem(listelement, "value"), stringsAsFactors = FALSE) names(d) &lt;- unlist(extractitem(listelement, "name")) d }) </code></pre> <p>rowlist is now a list of data frames; we can consolidate them into a single data frame with 'rbind'. The nice thing about using data frames in the previous step (as opposed to vectors or something with lower overhead) is that rbind will reorder the columns if necessary, so if the order of the fields changes from element to element, we're still all right.</p> <pre><code>finaldf &lt;- do.call("rbind", rowlist) </code></pre> <p>We still need to change the elements fo finaldf from "character" to whatever's appropriate for your application through, e.g.</p> <pre><code>finaldf$points &lt;- as.numeric(finaldf$points) </code></pre> <p>and so on. The last step cleans up the data frame by stripping the automatically-generated row names:</p> <pre><code>rownames(finaldf) &lt;- NULL </code></pre> <p>In case you need to tweak things, the general idea is to write a function that will format each ul[[i]] as a data frame with the correct column names; then invoke that function on each element of ul with lapply; and finally collapse the resulting list with do.call("rbind",...).</p>
 

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