Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd columns to a list of data.frames
    primarykey
    data
    text
    <p>This is a similar question to an old post of mine <a href="https://stackoverflow.com/questions/17286813/add-columns-to-a-dataframe-based-on-values-from-a-list">Add columns to a dataframe based on values from a list</a>.</p> <p>Now, I would like to use the solution provide it, but instead of applying it to a single data.frame, I'd like to use it on a list of data.frame.</p> <p>Briefly, I have a list of data.frames that looks like this:</p> <pre><code>df &lt;- list(data.frame(A=c("a","b","c"), B=c("1","2","1"), C=c(0.1,0.7,0.4)), data.frame(A=c("d","e","f"), B=c("2","2","3"), C=c(0.5,0.1,0.5)), data.frame(A=c("g","h","i"), B=c("3","1","2"), C=c(0.2,0.1,0.5))) </code></pre> <p>And a list with elements which names match to <code>df$B</code>, i.e, these values are permutations of values from <code>df$B</code>, here is an example:</p> <pre><code> ll &lt;- list('1'=c(0.1,0.1,0.4,0.2,0.1,0.4), '2'=c(0.1,0.1,0.5,0.7,0.5,0.7), '3'=c(0.1,0.1,0.2,0.2,0.2,0.5)) </code></pre> <p>I want to create a new list of data.frames but with new columns in each dataframe of list <code>df</code> that correspond to the values of <code>df$B</code> in list <code>ll</code> but at the same time they are <em>sampled</em> values from <code>ll</code>? Here is a desired output for a better explanation</p> <pre><code>&gt; list.df [[1]] A B C P1 P2 P3 P4 P5 P6 1 a 1 0.1 0.1 0.1 0.4 0.2 0.1 0.4 2 b 2 0.7 0.1 0.5 0.7 0.1 0.5 0.1 3 c 1 0.4 0.4 0.1 0.2 0.1 0.1 0.4 [[2]] A B C P1 P2 P3 P4 P5 P6 1 d 2 0.5 0.1 0.7 0.5 0.1 0.7 0.1 2 e 2 0.1 0.7 0.5 0.1 0.7 0.1 0.5 3 f 3 0.5 0.5 0.5 0.2 0.1 0.2 0.1 [[3]] A B C P1 P2 P3 P4 P5 P6 1 g 3 0.2 0.1 0.5 0.2 0.2 0.2 0.5 2 h 1 0.1 0.2 0.1 0.4 0.2 0.2 0.4 3 i 2 0.5 0.1 0.5 0.1 0.1 0.5 0.7 </code></pre> <p>The solution that I have for one single data.frame is this:</p> <pre><code>sampfun &lt;- function(i, l) sample(l[[as.character(i)]], 10000, replace=TRUE) list.df &lt;- cbind(df, t(sapply(df$B, sampfun, l = ll))) </code></pre> <p>The problem is that I don't know how to implement this solution for use with a list of data.frames.</p> <p>Many thanks for the help</p> <p>Note: my <em>real</em> list of data.frames has 9,000 elements, and I look to add more than 10,000 columns so the memory and speed up are important. </p>
    singulars
    1. This table or related slice is empty.
    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.
    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