0

Sorry if this a basic question. I've looked everywhere for help and can't seem to find an answer. Basically I have two data frames, one is a "Answer Key" with the answers starting in column 3.

and a master data frame with the answers of fifty students.

The student ID is in column one with the answers starting in column three.

How do I construct a for loop to return a vector or data frame with TRUEs or FALSEs for if they got the question right or wrong? Thank you for the help! This has had me stumped for a week

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • I've tried to use relational operators inside for loops that our instructor told us to study out of Data Camp. I remember that smaller dataframe will loop over a larger vector repeatedly until it's done but I can't find how to start the loop from the third column or compare. I'm really 100% lost – Noob Statistician Feb 23 '18 at 10:22
  • 3
    Do not post your data as an image, please learn how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610) – Jaap Feb 23 '18 at 10:34

2 Answers2

0

If you really want to use loops in R:

all_results <- NULL
for (i in 1:nrow(results)){
answer <- cbind(results[i,1], key[3:ncol(key)] == results[i,3:ncol(results)])
all_results <- rbind(all_results, answer)
}

This should give you a table with the student ID in the first column and whether their answers were true or false for every question.

0

Consider transposing the data and doing column comparison. Small example using data.table:

library(data.table)
set.seed(420666)
temp <- data.table("ASD" = sample(letters), "student1" = sample(letters), "student2" = sample(letters))
temp[1:5, student2 := ASD]
temp[, .SD == ASD, .SDcols = c("student1", "student2")]
      student1 student2
 [1,]    FALSE     TRUE
 [2,]    FALSE     TRUE
 [3,]    FALSE     TRUE
 [4,]    FALSE     TRUE
 [5,]    FALSE     TRUE
 [6,]    FALSE    FALSE
 [7,]    FALSE    FALSE
 [8,]    FALSE    FALSE
 [9,]    FALSE     TRUE
[10,]    FALSE    FALSE
[11,]    FALSE    FALSE
[12,]    FALSE    FALSE
[13,]    FALSE    FALSE
[14,]    FALSE    FALSE
[15,]    FALSE    FALSE
[16,]    FALSE    FALSE
[17,]    FALSE    FALSE
[18,]    FALSE    FALSE
[19,]    FALSE    FALSE
[20,]    FALSE    FALSE
[21,]     TRUE    FALSE
[22,]    FALSE    FALSE
[23,]    FALSE    FALSE
[24,]    FALSE    FALSE
[25,]    FALSE    FALSE
[26,]    FALSE    FALSE
UpsideDownRide
  • 528
  • 1
  • 4
  • 12