Dear Arun,,

Thank you. its perfect! wow! thank you very much..and David, thank you for
you too.. its such a help. I am so sorry it must've been confusing at the
beginning..
really, I dont know how to thank you..

well do you mind if I ask you how can you be so expert? what kind a book or
training did you have? and how long have you been working on R?
I am really interested in R


On Sun, Jul 28, 2013 at 2:40 AM, arun <smartpink111@yahoo.com> wrote:

> If you wanted to wrap it in a function:
>
>
> fun1<- function(dat,colName,newColumn){
>       indx<- which(dat[,colName]=="buy")
>       dat[,newColumn]<-0
>       dat[unique(unlist(lapply(seq_along(indx),function(i){
>             x1<- if(i==length(indx)){
>                 seq(indx[i],nrow(dat))
>              }
>             else if((indx[i+1]-indx[i])==1){
>             indx[i]
>             }
>             else {
>             seq(indx[i]+1,indx[i+1]-1)
>              }
>             x2<- dat[unique(c(indx[i:1],x1)),]
>             x3<- subset(x2,response=="sample")
>             x4<- subset(x2,response=="buy")
>             x4New<-x4[order(as.numeric(row.names(x4))),]
>             x5<- row.names(x4New)[duplicated(x4New$product)]
>             x6<- if(nrow(x3)!=0) {
>                             row.names(x3)[x3$product%in% x4$product]
>                        }
>
>             sort(as.numeric(c(x5,x6)))
>             }))),newColumn] <- 1
>     dat
>
>     }
>
>
>  fun1(tt1,"response","newCol")
> #   subj response product newCol
> #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
> #11    5      buy       4      1
> #12    5   sample       2      1
> #13    5      buy       2      1
> #14    6      buy       4      1
> #15    6   sample       5      0
> #16    6   sample       5      0
> #17    7   sample       4      1
> #18    7      buy       3      1
> #19    7      buy       4      1
> #20    8      buy       5      0
> #21    8   sample       4      1
> #22    8      buy       2      1
>
> A.K.
>
>
> ----- Original Message -----
> From: arun <smartpink111@yahoo.com>
> To: vanessa van der vaart <vanessa.vaart@gmail.com>
> Cc: David Winsemius <dwinsemius@comcast.net>; R help <r-help@r-project.org
> >
> Sent: Saturday, July 27, 2013 9:11 PM
> Subject: Re: [R] Duplicated function with conditional statement
>
> HI,
> May be this is what you wanted.
> #using tt1
> indx<-which(tt1$response=="buy")
> tt1$newcolumn<-0
> tt1[unique(unlist(lapply(seq_along(indx),function(i){x1<-if(i==length(indx))
> seq(indx[i],nrow(tt1)) else if((indx[i+1]-indx[i])==1) indx[i] else
> seq(indx[i]+1,indx[i+1]-1);x2<-
> tt1[unique(c(indx[1:i],x1)),];x3<-subset(x2,response=="sample");x4<-
> subset(x2,response=="buy");
> x5<-row.names(x4)[duplicated(x4$product)];x6<-if(nrow(x3)!=0)
> row.names(x3)[x3$product%in% x4$product];sort(c(x5,x6))}))),"newcolumn"]<-1
>
>
>  tt1
>    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
> 11    5      buy       4         1
> 12    5   sample       2         1
> 13    5      buy       2         1
> 14    6      buy       4         1
> 15    6   sample       5         0
> 16    6   sample       5         0
> 17    7   sample       4         1
> 18    7      buy       3         1
> 19    7      buy       4         1
> 20    8      buy       5         0
> 21    8   sample       4         1
> 22    8      buy       2         1
> A.K.
>
>
>
>
>
> ________________________________
> From: vanessa van der vaart <vanessa.vaart@gmail.com>
> To: arun <smartpink111@yahoo.com>
> Cc: David Winsemius <dwinsemius@comcast.net>; R help <r-help@r-project.org
> >
> Sent: Saturday, July 27, 2013 6:55 PM
> Subject: Re: [R] Duplicated function with conditional statement
>
>
>
> Dear all,,
> thank you all for your help..Its been such a help but its not really
> exactly what I am looking for. Apparently I havent explained the condition
> very clearly. I hope this can works.
>
> If the data on column product is duplicated from the previous row, (its
> applied for response==buy and ==sample) , and it is duplicated from the row
> which has the value on column 'response'== buy, than  the value = 1,
> otherwise is =0.
> so in that case,
> if the value is duplicated but it is duplicated from the previous row
> where the value of resonse==sample, than it is not considered duplicated,
> and in the new column is 0
>
> thank you very much in advance,
> I really appreciated
>

	[[alternative HTML version deleted]]

