hi Arun,

Thanks for this. This solution works great.

Knid Regards

Anup


On Sun, Jul 14, 2013 at 8:07 PM, arun <smartpink...@yahoo.com> wrote:

> Hi,
> You could try this: (if I understand it correctly)
> dat1<- read.table(text="
> year    id var ans
>  2010  1  1  1
>  2010  2  0  0
>  2010  1  0  1
> 2010  1  0  1
>  2011  2  1  1
>  2011  2  0  1
>  2011  1  0  0
> 2011  1  0  0
> ",sep="",header=TRUE,stringsAsFactors=FALSE)
>
> dat1$newres<-with(dat1,ave(var,id,year,FUN=function(x) any(x==1)*1))
>  dat1
> #  year id var ans newres
> #1 2010  1   1   1      1
> #2 2010  2   0   0      0
> #3 2010  1   0   1      1
> #4 2010  1   0   1      1
> #5 2011  2   1   1      1
> #6 2011  2   0   1      1
> #7 2011  1   0   0      0
> #8 2011  1   0   0      0
>
> A.K.
>
> ----- Original Message -----
> From: Anup Nandialath <anupme...@gmail.com>
> To: r-help@r-project.org
> Cc:
> Sent: Sunday, July 14, 2013 7:30 AM
> Subject: [R] creating dummy variables based on conditions
>
> Hello everyone,
>
> I have a dataset which includes the first three variables from the demo
> data below (year, id and var). I need to create the new variable ans as
> follows
>
> If var=1, then for each year (where var=1), i need to create a new dummy
> ans which takes the value of 1 for all corresponding id's where an instance
> of one was recorded. Sample data with the output is shown below.
>
>     year    id var ans
> [1,] 2010  1   1   1
> [2,] 2010  2   0   0
> [3,] 2010  1   0   1
> [4,] 2010  1   0   1
> [5,] 2011  2   1   1
> [6,] 2011  2   0   1
> [7,] 2011  1   0   0
> [8,] 2011  1   0   0
>
> Any help on how to achieve this is much appreciated.
>
> Thanks
> Anup
>
>     [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to