Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing paste to write a loop in R?
    primarykey
    data
    text
    <p>I'm trying to write this piece of code using a for loop.</p> <pre><code>#Took Quiz X and 1 TookQuizX[1,1] &lt;- nrow(Q1[Q1$anon_user_id %in% Q1$anon_user_id,]) TookQuizX[2,1] &lt;- nrow(Q2[Q2$anon_user_id %in% Q1$anon_user_id,]) TookQuizX[3,1] &lt;- nrow(Q3[Q3$anon_user_id %in% Q1$anon_user_id,]) TookQuizX[4,1] &lt;- nrow(Q4[Q4$anon_user_id %in% Q1$anon_user_id,]) TookQuizX[5,1] &lt;- nrow(Q5[Q5$anon_user_id %in% Q1$anon_user_id,]) TookQuizX[6,1] &lt;- nrow(Q6[Q6$anon_user_id %in% Q1$anon_user_id,]) </code></pre> <p>What I tried is the following</p> <pre><code>for(i in 1:6){ Qx&lt;-paste("Q",i,"[Q",i,"$anon_user_id",sep="") TookQuizX[i,1] &lt;- nrow(Qx %in% Q1$anon_user_id,]) } </code></pre> <p>When I run my loop I get the following error: </p> <pre><code>Error: unexpected ']' in: " Qx&lt;-paste("Q",i,"[Q",i,"$anon_user_id",sep="") TookQuizX[i,1] &lt;- nrow(Qx %in% Q1$anon_user_id,]" &gt; } Error: unexpected '}' in "} </code></pre> <p>What am I doing wrong?</p> <p>Thanks!</p> <hr> <p>This very simple example hopefully illustrates what i'm trying to do</p> <pre><code>TookQuizX &lt;- matrix(data=NA,nrow=3,ncol=1) Q1 &lt;- data.frame(anon_user_id = c("A123", "A111", "A134", "A156"), other_stuf=999) Q2 &lt;- data.frame(anon_user_id = c("A123", "A234", "A111", "A256", "C521"), other_stuf=999) Q3 &lt;- data.frame(anon_user_id = c("A123", "A234", "A111", "A356", "B356"), other_stuf=999) TookQuizX[1,1] &lt;- nrow(Q1[Q1$anon_user_id %in% Q1$anon_user_id,]) TookQuizX[2,1] &lt;- nrow(Q2[Q2$anon_user_id %in% Q1$anon_user_id,]) TookQuizX[3,1] &lt;- nrow(Q3[Q3$anon_user_id %in% Q1$anon_user_id,]) </code></pre>
    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