Note that there are some explanatory texts on larger screens.

plurals
  1. POR Using a for() loop to fill one dataframe with another
    primarykey
    data
    text
    <p>I have two dataframes and I wish to insert the values of one dataframe into another (let's call them <code>DF1</code> and <code>DF2</code>).</p> <p><code>DF1</code> consists of 2 columns 1 and 2. Column 1 (<code>col1</code>) contains characters a to z and <code>col2</code> has values associated with each character (from a to z)</p> <p><code>DF2</code> is a dataframe with 3 columns. The first two consist of every combination of <code>DF1$col1</code> so: aa ab ac ad etc; where the first letter is in <code>col1</code> and the second letter is in <code>col2</code></p> <p>I want to create a simple mathematical model utilizing the values in <code>DF1$col2</code> to see the outcomes of every possible combination of objects in <code>DF1$col1</code> The first step I wanted to do is to transfer values from <code>DF1$col2</code> to <code>DF2$col3</code> (values from <code>DF2$col3</code> should be associated to values in DF2col1), but that's where I'm stuck. I currently have </p> <pre><code>for(j in 1:length(DF2$col1)) { ## this part is to use the characters in DF2$col1 as an input ## to yield the output for DF2$col3-- input=c(DF2$col1)[j] ## This is supposed to use the values found in DF1$col2 to fill in DF2$col3 g=DF1[(DF1$col2==input),"pred"] ## This is so that the values will fill in DF2$col3-- DF2$col3=g } </code></pre> <p>When I run this, <code>DF2$col3</code> will be filled up with the same value for a specific character from <code>DF1</code> (e.g. <code>DF2$col3</code> will have all the rows filled with the value associated with character "a" from <code>DF1</code>) What exactly am I doing wrong?</p> <p>Thanks a bunch for your time</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.
 

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