Quick question: I am running the following code on some variables that are factors: dbpmn$IID1new <- ifelse(as.character(dbpmn[,2]) =as.character(dbpmn[,(21)]), dbpmn[,20], '') Instead of returning some value it gives me this: c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)) Playing around with the code, gives me some kind of variation to it. Is there some way to get me what I want. The variable that its suppose to give back is a bunch of sampleIDs. Thanks!
Not reproducible, ball in your court. However, in the meantime, my suggestion is to not do that. Convert to character before you alter the factor, then convert back when you are done. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On September 27, 2014 9:49:41 PM PDT, Kate Ignatius <kate.ignatius at gmail.com> wrote:>Quick question: > >I am running the following code on some variables that are factors: > >dbpmn$IID1new <- ifelse(as.character(dbpmn[,2]) =>as.character(dbpmn[,(21)]), dbpmn[,20], '') > >Instead of returning some value it gives me this: > >c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)) > >Playing around with the code, gives me some kind of variation to it. >Is there some way to get me what I want. The variable that its >suppose to give back is a bunch of sampleIDs. > >Thanks! > >______________________________________________ >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.
On Sun, 28 Sep 2014 12:49:41 AM Kate Ignatius wrote:> Quick question: > > I am running the following code on some variables that are factors: > > dbpmn$IID1new <- ifelse(as.character(dbpmn[,2]) => as.character(dbpmn[,(21)]), dbpmn[,20], '') > > Instead of returning some value it gives me this: > > c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)) > > Playing around with the code, gives me some kind of variation to it. > Is there some way to get me what I want. The variable that its > suppose to give back is a bunch of sampleIDs. >Hi Kate, If I create a little example: dbpmn<-data.frame(V1=factor(sample(LETTERS[1:4],20,TRUE)), V2=factor(sample(LETTERS[1:4],20,TRUE)), V3=factor(sample(LETTERS[1:4],20,TRUE))) dbpmn[4]<- ifelse(as.character(dbpmn[,1]) == as.character(dbpmn[,(2)]), dbpmn[,3],"") dbpmn V1 V2 V3 V4 1 B D C 2 C A D 3 C B A 4 A B C 5 B D B 6 D D A 1 7 D D D 4 8 B C A 9 B D B 10 D C A 11 A D C 12 A C B 13 A A A 1 14 D C A 15 C D B 16 A A B 2 17 A C C 18 B B C 3 19 C C C 3 20 D D D 4 I get what I expect, the numeric value of the third element in dbpmn where the first two elements are equal. I think what you want is: dbpmn[4]<- ifelse(as.character(dbpmn[,1]) == as.character(dbpmn[,(2)]), as.character(dbpmn[,3]),"") dbpmn V1 V2 V3 V4 1 B D C 2 C A D 3 C B A 4 A B C 5 B D B 6 D D A A 7 D D D D 8 B C A 9 B D B 10 D C A 11 A D C 12 A C B 13 A A A A 14 D C A 15 C D B 16 A A B B 17 A C C 18 B B C C 19 C C C C 20 D D D D Jim
I believe you are in Circle 8.2.7 of The R Inferno. http://www.burns-stat.com/documents/books/the-r-inferno/ Pat On 28/09/2014 05:49, Kate Ignatius wrote:> Quick question: > > I am running the following code on some variables that are factors: > > dbpmn$IID1new <- ifelse(as.character(dbpmn[,2]) => as.character(dbpmn[,(21)]), dbpmn[,20], '') > > Instead of returning some value it gives me this: > > c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)) > > Playing around with the code, gives me some kind of variation to it. > Is there some way to get me what I want. The variable that its > suppose to give back is a bunch of sampleIDs. > > Thanks! > > ______________________________________________ > 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. >-- Patrick Burns pburns at pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming')