[R] Creating variables with different levels depending on conditions

Jim Lemon jim at bitwrit.com.au
Mon Mar 16 12:28:25 CET 2009

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<-ifelse(task_2 == task,1,2)

and you can probably do all the values at once by vectorizing this.


More information about the R-help mailing list