Here is a very simple toy data set to illustrate a problem that I am currently encountering with another data set.
Suppose we have tested 4 participants in a math test, where each of them answered 4 questions. 2 of these questions were easy, and 2 of them were difficult. But the questions were presented in random order, so some people started with an easy question and some people started with a difficult question. And we have a binary response variable in this experiment, where we classified the answers as either "correct" or "incorrect."
Here is the fake data:
my_matrix <- matrix(c(rep(1:4, each=4), rep(1:4, 4), rep(c("difficult", "easy"), times = 4), rep(c("easy", "difficult"), times = 4), rep(c("correct", "incorrect"), times = 8)), nrow=16, ncol=4, byrow = FALSE)
my_matrix
my_data_frame <- as.data.frame(my_matrix)
colnames(my_data_frame) <- c("Participant", "ItemNumber", "QuestionDifficulty", "Answer")
my_data_frame$Participant <- as.numeric(my_data_frame$Participant)
my_data_frame
Now, I want to create a new column such that its value is "DifficultFirst" for people who started with a difficult question and "EasyFirst" for people who started with an easy question. I tried the following code for this.
for (i in 1:16) {
ifelse(my_data_frame$Participant == i & my_data_frame$ItemNumber == 1 & my_data_frame$QuestionDifficulty =="difficult",
my_data_frame$FirstQuestion[((i*4)-3):(i*4)] <- "DifficultFirst",
my_data_frame$FirstQuestion[((i*4)-3):(i*4)] <- "EasyFirst")}
But it didn't work. Specifically, I got an error message about the replacement and the data not matching in terms of their row numbers, and I don't know why this is the case.
It is late in the day and my brain may be too tired, so apologies if this is a silly question. But any help would be appreciated. Thank you!
Aucun commentaire:
Enregistrer un commentaire