Skip to content
Prev 327455 / 398502 Next

duplicated() with conditional statement

Hi,
You may try this (didn't get time to test this extensively)
indx<-which(tt$response!="buy")
tt$newcolumn<-0
?tt[unlist(lapply(seq_along(indx),function(i) {x1<-if(indx[i]==nrow(tt)) indx[i] else seq(indx[i]+1,indx[i+1]-1);x2<-rbind(tt[indx[1:i],],tt[x1,]); if(any(x2$response=="sample")) row.names(x2[duplicated(x2$product),])})),"newcolumn"]<-1
?tt
#?? subj response product newcolumn
#1???? 1?? sample?????? 1???????? 0
#2???? 1?? sample?????? 2???????? 0
#3???? 1????? buy?????? 3???????? 0
#4???? 2?? sample?????? 2???????? 0
#5???? 2????? buy?????? 2???????? 0
#6???? 3?? sample?????? 3???????? 1
#7???? 3?? sample?????? 2???????? 1
#8???? 3????? buy?????? 1???????? 0
#9???? 4?? sample?????? 1???????? 1
#10??? 4????? buy?????? 4???????? 0
A.K.
Message-ID: <1374789138.80385.YahooMailNeo@web142604.mail.bf1.yahoo.com>
In-Reply-To: <CAEQiC4iOB9CP5MsbOS185KpyOzjL3etysX2=30=qmQxNEJONFg@mail.gmail.com>