Miriam Gade
2009-Mar-16 10:11 UTC
[R] Creating variables with different levels depending on conditions
Dear R-help-list, I started using R coming from the former use of SPSS, mainly writing my own syntax. I do classical experimental cognitive psychology and am interested in the cognitive control of task sequences (i.e. classification task). Especially I am interested in the performance of the actual depending on the tasks before. Thus, in SPSS I create variables like this one: Compute cond = 0. If (task_2=task) cond=1 else cond=2. task refers to the actual task and task_2 to the task performed 2 trials before, cond=0 is used later to filter off the first two tasks in every block. So far, I could create all variables I need in R like in SPSS. However, I don't succeed creating a variable like "cond (=condition)" in R. I managed to create a variable that is either true or false, but I would prefer a categorical variable to get a table which I can use later for t-test and anovas. I am bit puzzled that this simple syntax in SPSS has no equivalent in R or am I just to blind to find it? All suggestions are cordially welcome, miriam Dr. Miriam Gade University of Zuerich Institute of Psychology, General Psychology (Cognition) Binzmuehlestr. 14, Box 22 CH-8050 Zuerich Phone: +41 (0)44 6357-461
Jim Lemon
2009-Mar-16 11:28 UTC
[R] Creating variables with different levels depending on conditions
Miriam Gade wrote:> Dear R-help-list, > > I started using R coming from the former use of SPSS, mainly writing my own > syntax. > I do classical experimental cognitive psychology and am interested in the > cognitive control of task sequences (i.e. classification task). Especially I > am interested in the performance of the actual depending on the tasks > before. Thus, in SPSS I create variables like this one: > > Compute cond = 0. > If (task_2=task) cond=1 else cond=2. > > task refers to the actual task and task_2 to the task performed 2 trials > before, cond=0 is used later to filter off the first two tasks in every > block. > > So far, I could create all variables I need in R like in SPSS. However, I > don't succeed creating a variable like "cond (=condition)" in R. I managed > to create a variable that is either true or false, but I would prefer a > categorical variable to get a table which I can use later for t-test and > anovas. I am bit puzzled that this simple syntax in SPSS has no equivalent > in R or am I just to blind to find it? All suggestions are cordially > welcome, miriam > >Hi Miriam, Try this: cond<-0 cond<-ifelse(task_2 == task,1,2) and you can probably do all the values at once by vectorizing this. Jim