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]]
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) <- NULL res Hope this helps, Rui Barradas Em 14-07-2013 12:30, Anup Nandialath escreveu:> 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 at 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. >
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 <anupmenon at gmail.com> To: r-help at 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 at 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.