I checked the other questions and couldn't really find the solution.
I'm trying to update a vector in my dataframe based on a BUNCH of conditionals like the following:
EEG3$CorrectFinal <-as.character(EEG3$CorrectFinal)
subjectlist1 = c("s002","s004","s010","s011","s012","s017","s018","s019","s020","s025","s026","s028")
for (i in subjectlist1) {
EEG3$CorrectFinal <- ifelse (EEG3$subject == subjectlist1[i] & EEG3$trial <326 & EEG3$Deviant == 101 & EEG3$Correct == 222|EEG$subject == subjectlist1[i] & EEG3$trial <326 & EEG3$Deviant == 102 & EEG3$Correct == 155|EEG3$subject == subjectlist1[i] & EEG3$trial >326 & EEG3$Deviant == 101 & EEG3$Correct == 155|EEG$subject == subjectlist1[i] & EEG3$trial >326 & EEG3$Deviant == 102 & EEG3$Correct == 222, yes = 1, no = EEG3$CorrectFinal)}
But then I get the following error message: Error in `$<-.data.frame`(`*tmp*`, "CorrectFinal", value = logical(0)) : replacement has 0 rows, data has 20
So I've created an vector (column) of zeroes in the data frame, and assign the conditionals to either, if true, populate with a 1, if not, keep the same value (which is a 0). I just don't understand why I'm getting this error message, or where this script is breaking.
Here's some code to reproduce the problem with an example data frame:
EEG3 <-read.table(header = TRUE, text =" subject trial Deviant Correct CorrectFinal
1 s002 1 101 102 0
2 s002 2 102 101 0
3 s002 3 101 155 0
4 s002 4 101 155 0
5 s002 5 101 155 0
6 s002 6 101 155 0
7 s002 7 102 222 0
8 s002 8 101 155 0
9 s002 9 101 155 0
10 s002 10 102 222 0
11 s002 11 101 222 1
12 s002 12 101 222 1
13 s002 13 101 222 1
14 s002 14 101 222 1
15 s002 15 102 155 1
16 s002 16 101 222 1
17 s002 17 101 155 0
18 s002 18 101 222 1
19 s002 19 102 155 1
20 s002 20 101 222 1")
> ```
Aucun commentaire:
Enregistrer un commentaire