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:> laladir 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 -- Karin Lagesen, PhD student karin.lagesen at medisin.uio.no http://folk.uio.no/karinlag
Hi,> An I misunderstanding how to use the ifelse construct? And in that > case, how should I go about doing this in a different way?The ifelse function only apply to a single test passed as first argument, and the second and third arguments are the value returned, and thus should not be an R instruction like ?gc_content <- -gc_content?. Maybe you could try the following : dataframe$numdir <- 1 dataframe$numdir[dataframe$dir=="-"] <- -1 d <- dataframe$gc_content * dataframe$numdir HTH, Julien -- Julien Barnier Groupe de recherche sur la socialisation ENS-LSH - Lyon, France
Karin Lagesen wrote:> 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 >Yes, you are misunderstanding it and you should study the examples on the help page. One key point is that (in general) both the 'yes' and 'no' get evaluated, so having assignments to the same variable in there is a really bad idea. Another point is that ifelse takes three _vectors_ and returns a fourth. I'd do result <- ifelse(dir=="-", -gc_content, gc_content) -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
On 9/25/07, Karin Lagesen <karin.lagesen at medisin.uio.no> wrote:> > 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 > --Hej igen! The ifelse(x,a,b) returns a vector whose elements are picked from either a or b depending on whether x is true or false. However it evaluates both the a and the b vector. Since you are changing gc_content in both a and b, strange things are bound to happen. The easiest way would be to just skip the assignment in the ifelse construct. Like so: changedir <- function(dataframe) { dir <- dataframe$dir gc_content <- dataframe$gc_content d <- ifelse(dir == "-", -gc_content,gc_content) return(d) } Hope it helps! best, Gustaf -- Gustaf Rydevik, M.Sci. tel: +46(0)703 051 451 address:Essingetorget 40,112 66 Stockholm, SE skype:gustaf_rydevik
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
The thing to remember is that ifelse is a function, it returns a value, so usually one does not need assignments within the function call. You also could do: negindices <- dir=="-" gc_content[negindices] <- -gc_content[negindices] Eik Vettorazzi wrote:> 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 >-- Erich Neuwirth, University of Vienna Faculty of Computer Science Computer Supported Didactics Working Group Visit our SunSITE at http://sunsite.univie.ac.at Phone: +43-1-4277-39464 Fax: +43-1-4277-39459