[R] Am I misunderstanding the ifelse construction?

Eik Vettorazzi E.Vettorazzi at uke.uni-hamburg.de
Tue Sep 25 10:39:23 CEST 2007


d <- ifelse(dir == "-", -gc_content , gc_content)
works. You need not assign gc_content a new value in each comparison,  
because then your "result" depends only on the last value of "dir", 
which happened to be "-", so you got -0.5 for all gc_content.

hth


Karin Lagesen schrieb:
> I have a function like this:
>
> changedir <- function(dataframe) {
> dir <- dataframe$dir
> gc_content <- dataframe$gc_content
> d <- ifelse(dir == "-",
>             gc_content <- -gc_content,gc_content <- gc_content)
> return(d)
> }
>
> The goal of this function is to be able to input a data frame like this:
>
>
>   
>> lala
>>     
>    dir gc_content
> 1    +        0.5
> 2    -        0.5
> 3    +        0.5
> 4    -        0.5
> 5    +        0.5
> 6    -        0.5
> 7    +        0.5
> 8    -        0.5
> 9    +        0.5
> 10   -        0.5
> 11   +        0.5
> 12   -        0.5
> 13   +        0.5
> 14   -        0.5
> 15   +        0.5
> 16   -        0.5
> 17   +        0.5
> 18   -        0.5
> 19   +        0.5
> 20   -        0.5
>   
>
> And change the sign of the value of the gc_content field if the
> corresponding dir field is negative.
>
> Howver, when I run this through the changedir function, all of the
> gc_contents become negative.
>
> An I misunderstanding how to use the ifelse construct? And in that
> case, how should I go about doing this in a different way?
>
> Thankyou very much in advance for your help, and I hope that my
> question is not too banal!
>
> Karin
>   


-- 
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/42803-8243
F ++49/40/42803-7790



More information about the R-help mailing list