Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As with many operations in R, it is easier to wrap your data frames in a list.</p> <pre><code>Q_all &lt;- list(Q1,Q2,Q3) </code></pre> <p>First, instead of using <code>nrow</code>, why don't you directly measure how many <code>TRUE</code> values there are in your <code>%in%</code> vector.</p> <pre><code>TookQuizX[1,1] &lt;- length(which(Q1$anon_user_id %in% Q1$anon_user_id)) </code></pre> <p>To replace your loop, here is an example of <code>lapply</code>:</p> <pre><code>TookQuizX[,1] &lt;- unlist(lapply(Q_all, function(x) length(which(x$anon_user_id %in% Q_all[[1]]$anon_user_id)))) </code></pre> <p>I assume that in the end, you want <code>TookQuizX</code> to be a matrix where entry <code>i,j</code> is the number of people who took Quiz <code>i</code> and also took Quiz <code>j</code>. Additionally, I assume that your user ID's are unique, and no two rows in the data frame have the same user ID. Then let's extract just the user ID's from your data frames.</p> <pre><code>anon_user_ids &lt;- lapply(Q_all, `[[`, "anon_user_id") </code></pre> <p>One way of putting this together (and there are more efficient ways, but this is what came to mind first) would be to <code>Map</code>:</p> <pre><code>tmp &lt;- Map(function(x,y) length(which(x %in% y)), anon_user_ids[rep(seq_along(anon_user_ids),times = length(anon_user_ids))] , anon_user_ids[rep(seq_along(anon_user_ids),each = length(anon_user_ids))] ) </code></pre> <p>This compares the intersection of <code>i</code> and <code>j</code> iteratively, so <code>1,1</code>, <code>2,1</code>, <code>3,1</code>, <code>1,2</code>, <code>2,2</code> and so forth. Now I can put this into a matrix. By default in matrices and arrays in R, vectors are assumed to be in column-major order (the first dimension varies quickest, and the last dimension varies slowest).</p> <pre><code>TookQuizX &lt;- matrix(unlist(tmp), nrow = length(anon_user_ids)) # [,1] [,2] [,3] # [1,] 4 2 2 # [2,] 2 5 3 # [3,] 2 3 5 </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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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