Re: [R] adding a dummy variable...

2011-10-04 Thread baptiste auguie
Hi, Using ddply, ddply(df, .(ID), mutate, nrows=length(rel.head), test = nrows==2 & all(rel.head %in% c(1,3))) HTH, baptiste On 5 October 2011 06:02, Dennis Murphy wrote: > Hi: > > Here's another way to do it with the plyr package, also not terribly > elegant. It assumes that rel.head is a f

Re: [R] adding a dummy variable...

2011-10-04 Thread Dennis Murphy
Hi: Here's another way to do it with the plyr package, also not terribly elegant. It assumes that rel.head is a factor in your original data frame: > str(df) 'data.frame': 11 obs. of 2 variables: $ ID : Factor w/ 6 levels "17100","17101",..: 1 1 2 3 4 4 5 5 5 6 ... $ rel.head: Factor w/

Re: [R] adding a dummy variable...

2011-10-04 Thread Martyn Byng
Hi, I am sure there are better / more efficient ways of doing this, but the following seems to work ... ids <- sapply(split(df,df$ID),function(x) {length(x$rel.head)==2 & any(x$rel.head==1) & any(x$rel.head==3)}) ids <- as.numeric(names(ids)[ids]) added.dummy <- as.numeric(df$ID%in%ids) cbind(df