vendredi 28 octobre 2016

Can I use a function as a return argument in ifelse in "R"

I'm currently working on my Master thesis in biology, researching a matching-to-sample task in monkeys. I want to evaluate how often a certain stimulus was chosen, regardless of correctness of the choice.
To do so, I have a dataframe, that has 6288 rows and six columns ("Monkey", "Session", "Sample", "Match", "Foil", "Success"), of which only the last three are important now. Just explaining so you get the general idea. The data in "Match" and "Foil" are the names of the stimuli (string) and "Success" is binary. "Match" and "Foil" are made up of 65 distinct stimuli names, which I included in a vector "Match.Foil". Now I want to count how often a picture (part of the vector "Match.Foil") is clicked in all 6288 trials. That is, everytime the name is either part of "Match" and "Success" == "1" OR when the name is part of "Foil" and "Success" == "0". I tried to build a vector with the number of times clicked for each part of Match.Foil like this:

Pic.clicked= vector(mode="numeric", length= length(Match.Foil))

for (i in 1:length(Match.Foil)) { Pic.clicked[i]=ifelse( (Stage4.pics$Match == Match.Foil[i] & Stage4.pics$Success == "1")| (Stage4.pics$Foil== Match.Foil[i] & Stage4.pics$Success == "0"), (Pic.clicked[i] +1), (Pic.clicked[i] +0))}

So, as you see I wanted to use the functions (Pic.clicked +1) and (Pic.clicked+0) as the returns if the statement is true or false. It does not work and gives me the error: In Pic.clicked[i] = ifelse((Stage4.pics$Match == Match.Foil[i] & ... : number of items to replace is not a multiple of replacement length Does anybody have an idea, how to build an appropriate counter? I thought about using "switch", but I don't have any experience with that function and it seems not to work like I need it. I also tried running it for 6288 loops, but that produces the ame warning... Thank you so much!

Aucun commentaire:

Enregistrer un commentaire