# Re: [R] simplifying the code

From: Jim Brennan <jfbrennan_at_rogers.com>
Date: Tue 28 Jun 2005 - 07:13:00 EST

This gives the same answer for what you have supplied so far...
> dat\$d<-ifelse((dat\$b-dat\$a)>=1000,dat\$b-dat\$a+1,0)
> dat

dear list:

I made some corrections in the previous post that had some mistakes.

I need help to achieve a simpler code to complete a task I'm performing.
here is an example:

dat<-expand.grid(a=seq(1,4),b=seq(1001,1004))

#I want to add a new column dat\$c in that:

```t1<-ifelse(dat\$a==1&dat\$b==1001,1001,0)
t2<-ifelse(dat\$a==2&dat\$b==1002,1001,0)
t3<-ifelse(dat\$a==3&dat\$b==1003,1001,0)
t4<-ifelse(dat\$a==4&dat\$b==1004,1001,0)
t5<-ifelse(dat\$a==1&dat\$b==1002,1002,0)
t6<-ifelse(dat\$a==2&dat\$b==1003,1002,0)
t7<-ifelse(dat\$a==3&dat\$b==1004,1002,0)
t8<-ifelse(dat\$a==1&dat\$b==1003,1003,0)
t9<-ifelse(dat\$a==2&dat\$b==1004,1003,0)
```

t10<-ifelse(dat\$a==1&dat\$b==1004,1004,0)

dat\$c<-t1+t2+t3+t4+t5+t6+t7+t8+t9+t10

My real data frame is much larger... I hope someone can help me with this.