Re: [R] Set value if else...

2010-10-15 Thread Rubén Roa
x <- data.frame(x=1:10)
require(gtools)
x$y <- ifelse(odd(x$x),0,1)
HTH

R.

 

Dr. Rubén Roa-Ureta
AZTI - Tecnalia / Marine Research Unit
Txatxarramendi Ugartea z/g
48395 Sukarrieta (Bizkaia)
SPAIN



> -Mensaje original-
> De: r-help-boun...@r-project.org 
> [mailto:r-help-boun...@r-project.org] En nombre de Joel
> Enviado el: viernes, 15 de octubre de 2010 10:16
> Para: r-help@r-project.org
> Asunto: [R] Set value if else...
> 
> 
> 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
> maja40
> 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 241
> agust   170
> maja401
> 
> And as I said before im guessing this can be done without a loop...
> 
> //Joel
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/Set-value-if-else-tp2996667p2996667.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> __
> 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.
> 

__
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.


Re: [R] Set value if else...

2010-10-15 Thread Joel

Indeed I was close :)

Thx for the fast respond!

Have a good day

//Joel
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Set-value-if-else-tp2996667p2996682.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


Re: [R] Set value if else...

2010-10-15 Thread Dimitris Rizopoulos

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
maja40
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 241
agust   170
maja401

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/

__
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.


Re: [R] Set value if else...

2010-10-15 Thread Joshua Wiley
Dear Joel,

On Fri, Oct 15, 2010 at 1: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

You're closer than you know:

table$VoteRight <- ifelse(table$age > 18, 1, 0)

?ifelse   is a vectorized function [ifelse(logical test, value if
TRUE, value if FALSE)], and you would not even need to cbind() it into
your table.

Josh

>
> 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
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/Set-value-if-else-tp2996667p2996667.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

__
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.


[R] Set value if else...

2010-10-15 Thread Joel

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
maja40
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 241
agust   170
maja401

And as I said before im guessing this can be done without a loop...

//Joel
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Set-value-if-else-tp2996667p2996667.html
Sent from the R help mailing list archive at Nabble.com.

__
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.