Skip to content
Back to formatted view

Raw Message

Message-ID: <OF8265A9EF.2E43FBD7-ONC1257A30.002E56CE-C1257A30.002EBAA2@precheza.cz>
Date: 2012-07-03T08:27:56Z
From: PIKAL Petr
Subject: Is it possible to remove this loop? [SEC=UNCLASSIFIED]
In-Reply-To: <8D2A822B1C6C2A44ABEB777AA83FA0E370A4B68A04@EXCCR01.agso.gov.au>

Hi

> Hi all,
> 
> I would like create a new column in a data.frame (a1) to store 0, 1 data 

> converted from a factor as below.
> 
> a1$h2<-NULL
> for (i in 1:dim(a1)[1]) {
>       if (a1$h1[i]=="H") a1$h2[i]<-1 else a1$h2[i]<-0
>       }
> 
> My question: is it possible to remove the loop from above code to 
achieve 
> the desired result?

Untested

a1$h2 <- (a1$h1=="H")*1

Regards
Petr

> 
> Thanks in advance,
> Jin
> 
> Geoscience Australia Disclaimer: This e-mail (and files transmitted with 

> it) is intended only for the person or entity to which it is addressed. 
If
> you are not the intended recipient, then you have received this e-mail 
by 
> mistake and any use, dissemination, forwarding, printing or copying of 
> this e-mail and its file attachments is prohibited. The security of 
emails
> transmitted cannot be guaranteed; by forwarding or replying to this 
email,
> you acknowledge and accept these risks.
> 
-------------------------------------------------------------------------------------------------------------------------
> 
> 
>    [[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.