[R] Set value if else...

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Fri Oct 15 10:25:13 CEST 2010


try this:

table$VoteRight <- as.numeric(table$age > 18)



Best,
Dimitris


On 10/15/2010 10:16 AM, Joel wrote:
>
> Hi
>
> I want to set a variable to either 1 or 0 depending on an value of a
> dataframe and then add this as a colum to the dataframe.
>
> This could be done with a loop but as we are able to do questions on a
> complete row or colum without a loop it would be sweet if it could be done.
>
> for example:
>
> table:
>
> Name  Age
> Joel     24
> agust   17
> maja    40
> and so on...
>
> And what I want to do is a command that gives me
> VoteRight<-1 if table$age>18 else set VoteRight to 0
>
> Then use cbind or something to add it to the table.
>
> so i get
> Name  Age  VoteRight
> Joel     24    1
> agust   17    0
> maja    40    1
>
> And as I said before im guessing this can be done without a loop...
>
> //Joel

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/



More information about the R-help mailing list