Yes thanks guys the pmin did the trick. Primitif maybe but effective.

On Fri, Nov 2, 2012 at 3:12 AM, David Winsemius [via R] <
ml-node+s789695n4648205...@n4.nabble.com> wrote:

>
> On Nov 1, 2012, at 6:38 PM, arun wrote:
>
> > Hi,
> >
> > You can try this also :
> > set.seed(132)
> >
>  
> dat1<-data.frame(Company=rep(LETTERS[1:4],c(3,4,5,6)),Last_year=sample(2005:2012,18,replace=TRUE))
>
> > dat2<-dat1[order(dat1$Company,dat1$Last_year),]
> > dat2$Last<-ave(dat2$Last_year,dat2$Company,FUN=function(x)
> ifelse(x>=2009,2009,x))
>
>  None of the `ifelse` conditional or desired consequent depend on the
> Company value. Just wasted CPU cycles are the result. If you choose to use
> `ifelse` , then why not:
>
> dat2$Last2<- ifelse(dat2$Last_year > 2009, 2009, dat2$Last_year)      #
> the '=' also not needed
>
>
> head(dat2)
>
> >
> > #  Company Last_year Last
> > #3       A      2007 2007
> > #1       A      2010 2009
> > #2       A      2012 2009
> > #6       B      2007 2007
> > #4       B      2008 2008
> > #5       B      2008 2008
> >
> > identical(dat2[,3],pmin(dat2[,2],2009))
> > #[1] TRUE
> >
> > identical(dat2[,4],pmin(dat2[,2],2009))
> [1] TRUE
>
> > A.K.
> >
> >
> >
> > ----- Original Message -----
> > From: kebrab67 <[hidden 
> > email]<http://user/SendEmail.jtp?type=node&node=4648205&i=0>>
>
> > To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4648205&i=1>
> > Cc:
> > Sent: Thursday, November 1, 2012 8:41 PM
> > Subject: [R] If loops?
> >
> > I have a set of data with  205 988 observation sand 10 variables , three
> of
> > which are Legal_status, Date_of_incorporation and Last_year. I set my
> time
> > horizon from 1989 to 2009. Now I want to know when a company is dead. If
> > Last_year is bigger or equal to 2009 then I say that a new "variable"
> last
> > is 2009. If Last_year is smaller than 2009 then my new variable "last"
> is
> > equal to the variable "Last_year". I wanted to know how I can generate
> this
> > new variable and connect it to my original data called data.
> >
> >
> >
> > --
> > View this message in context:
> http://r.789695.n4.nabble.com/If-loops-tp4648199.html
> > Sent from the R help mailing list archive at Nabble.com.
> >     [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] 
> > <http://user/SendEmail.jtp?type=node&node=4648205&i=2>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.
> >
>
> David Winsemius, MD
> Alameda, CA, USA
>
> ______________________________________________
> [hidden email] <http://user/SendEmail.jtp?type=node&node=4648205&i=3>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.
>
>
> ------------------------------
>  If you reply to this email, your message will be added to the discussion
> below:
> http://r.789695.n4.nabble.com/If-loops-tp4648199p4648205.html
>  To unsubscribe from If loops?, click 
> here<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4648199&code=c2VsYW1nZXRhY2hld0BnbWFpbC5jb218NDY0ODE5OXwtNTU1NzM1MTAx>
> .
> NAML<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>



-- 
Selam  Getachew
M1 Statistiques Econometrie
Responsable du Pole Communication AESES




--
View this message in context: 
http://r.789695.n4.nabble.com/If-loops-tp4648199p4648206.html
Sent from the R help mailing list archive at Nabble.com.
        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to