Re: [R] creating dummy variables based on conditions

2013-07-14 Thread Anup Nandialath
--- > From: Anup Nandialath > 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 (yea

Re: [R] creating dummy variables based on conditions

2013-07-14 Thread arun
   0 A.K. - Original Message - From: Anup Nandialath 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

Re: [R] creating dummy variables based on conditions

2013-07-14 Thread Rui Barradas
Hello, Your data seems to be of class 'matrix'. The following code needs it to be a data.frame. dat <- as.data.frame(your input matrix) res <- do.call(rbind, lapply(split(dat, list(dat$id, dat$year)), function(x){ x$ans <- if(any(x$var == 1)) 1 else 0 x})) rownames(res) <- N

[R] creating dummy variables based on conditions

2013-07-14 Thread Anup Nandialath
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'