[R] Dataframe and conditions

Frédéric Grelier fgrelier at weborama.com
Tue May 14 15:08:03 CEST 2013


Thanks a lot Guys
That works
I appreciate your helps

Best regards
Frederic

FREDERIC GRELIER – DIRECTEUR DATA
T +33 (0) 1 83 94 04 08 - P +33 (0) 6 70 50  01 05 - F +33 (0) 1 53 19 41
84
WWW.WEBORAMA.COM <http://www.weborama.com/>  - 15, RUE CLAVEL - 75019
PARIS




-----Message d'origine-----
De : arun [mailto:smartpink111 at yahoo.com]
Envoyé : mardi 14 mai 2013 15:19
À : Pascal Oettli
Cc : R help; fgrelier
Objet : Re: [R] Dataframe and conditions



#this should also work

 within(X,a<- ifelse(b,c,a))
#  a     b c
#1 2  TRUE 2
#2 2  TRUE 2
#3 1 FALSE 2
#4 1 FALSE 2
#5 1 FALSE 2
#6 2  TRUE 2
A.K.

----- Original Message -----
From: Pascal Oettli <kridox at ymail.com>
To: fgrelier <fgrelier at weborama.com>
Cc: r-help at r-project.org
Sent: Tuesday, May 14, 2013 4:47 AM
Subject: Re: [R] Dataframe and conditions

Hello,

One approach is using "ifelse":

> X <- data.frame(a=c(1,1,1,1,1,1),
b=c(TRUE,TRUE,FALSE,FALSE,FALSE,TRUE), c=c(2,2,2,2,2,2))
> X
   a     b c
1 1  TRUE 2
2 1  TRUE 2
3 1 FALSE 2
4 1 FALSE 2
5 1 FALSE 2
6 1  TRUE 2
>
> X <- within(X, a <- ifelse(b==TRUE, c, a)) X
   a     b c
1 2  TRUE 2
2 2  TRUE 2
3 1 FALSE 2
4 1 FALSE 2
5 1 FALSE 2
6 2  TRUE 2

Hope this helps,
Pascal


On 05/14/2013 05:06 PM, fgrelier wrote:
>
> I have in a dataframe X  :  3 Variables X$a , X$b, X$c I would like to
> replace in X the values of X$a by the values of  X$c but only when
> X$b=="TRUE"
> I have tried to put in place a loop but as I have a lot of rows it is
> very very long to run.
>
> Thanks for your help
>
>
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Dataframe-and-conditions-tp4667012.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>

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



More information about the R-help mailing list