HI All, I am having a little issue in my ifelse statement, The data frame looks like as follow. dat2 <-read.table(text="ID d1 d2 d3 A 0 25 35 B 12 22 0 C 0 0 31 E 10 20 30 F 0 0 0",header=TRUE,stringsAsFactors=F) I want to create d4 and set the value based on the following conditions. If d1 !=0 then d4=d1 if d1 = 0 and d2 !=0 then d4=d2 if (d1 and d2 = 0) and d3 !=0 then d4=d3 if all d1, d2 and d3 =0 then d4=0 Here is the desired output and my attempt ID d1 d2 d3 d4 A 0 25 35 25 B 12 22 0 12 C 0 0 31 31 E 10 20 30 10 F 0 0 0 0 0 My attempt dat2$d4 <- 0 dat2$d4 <- ifelse((dat2$d1 =="0"), dat2$d2, ifelse(dat2$d2 == "0"), dat2$d3, 0) but not working. Thank you.
I generally find nested ifelse's to be confusing and prone to error, so I usually prefer to proceed sequentially using subsetting with logicals or replicated, but not nested ifelse's. In your example, the translation to logical indexing seems pretty straightforward. Using your example:> dat2 <-within(dat2,{ d4 <- d1 ## d1. 0 when d1 == 0 d4[!d4]<- d2[!d4] d4[!d4]<- d3[!d4] })> dat2ID d1 d2 d3 d4 1 A 0 25 35 25 2 B 12 22 0 12 3 C 0 0 31 31 4 E 10 20 30 10 5 F 0 0 0 0 Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Tue, Nov 26, 2019 at 3:15 PM Val <valkremk at gmail.com> wrote:> HI All, I am having a little issue in my ifelse statement, > The data frame looks like as follow. > > dat2 <-read.table(text="ID d1 d2 d3 > A 0 25 35 > B 12 22 0 > C 0 0 31 > E 10 20 30 > F 0 0 0",header=TRUE,stringsAsFactors=F) > I want to create d4 and set the value based on the following conditions. > If d1 !=0 then d4=d1 > if d1 = 0 and d2 !=0 then d4=d2 > if (d1 and d2 = 0) and d3 !=0 then d4=d3 > if all d1, d2 and d3 =0 then d4=0 > > Here is the desired output and my attempt > ID d1 d2 d3 d4 > A 0 25 35 25 > B 12 22 0 12 > C 0 0 31 31 > E 10 20 30 10 > F 0 0 0 0 0 > > My attempt > dat2$d4 <- 0 > dat2$d4 <- ifelse((dat2$d1 =="0"), dat2$d2, ifelse(dat2$d2 == "0"), > dat2$d3, 0) > but not working. > > Thank you. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
Hi val, You had a "conditional leak" in your ifelse statements: dat2 <-read.table(text="ID d1 d2 d3 A 0 25 35 B 12 22 0 C 0 0 31 E 10 20 30 F 0 0 0", header=TRUE,stringsAsFactors=FALSE) dat2$d4<- ifelse(dat2$d1,dat2$d1,ifelse(dat2$d2,dat2$d2,ifelse(dat2$d3,dat2$d3,0))) Even though it works, it is probably better to use a string of "if" statements rather than the above. Jim On Wed, Nov 27, 2019 at 10:15 AM Val <valkremk at gmail.com> wrote:> > HI All, I am having a little issue in my ifelse statement, > The data frame looks like as follow. > > dat2 <-read.table(text="ID d1 d2 d3 > A 0 25 35 > B 12 22 0 > C 0 0 31 > E 10 20 30 > F 0 0 0",header=TRUE,stringsAsFactors=F) > I want to create d4 and set the value based on the following conditions. > If d1 !=0 then d4=d1 > if d1 = 0 and d2 !=0 then d4=d2 > if (d1 and d2 = 0) and d3 !=0 then d4=d3 > if all d1, d2 and d3 =0 then d4=0 > > Here is the desired output and my attempt > ID d1 d2 d3 d4 > A 0 25 35 25 > B 12 22 0 12 > C 0 0 31 31 > E 10 20 30 10 > F 0 0 0 0 0 > > My attempt > dat2$d4 <- 0 > dat2$d4 <- ifelse((dat2$d1 =="0"), dat2$d2, ifelse(dat2$d2 == "0"), dat2$d3, 0) > but not working. > > Thank you. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Hi Val, Here's an answer using a series of ifelse() statements. Because the d4 column is created initially using NA as a placeholder, you can check your conditional logic at the end using table(!is.na(dat2$d4)):> dat2 <-read.table(text="ID d1 d2 d3+ A 0 25 35 + B 12 22 0 + C 0 0 31 + E 10 20 30 + F 0 0 0", header=TRUE, stringsAsFactors=F)> > dat2$d4 <- NA > dat2$d4 <- with(dat2, ifelse(d1!=0, yes=d1, no=d4)) > dat2$d4 <- with(dat2, ifelse((d1==0 & d2!=0), yes=d2, no=d4)) > dat2$d4 <- with(dat2, ifelse((d1==0 & d2==0 & d3!=0), yes=d3, no=d4)) > dat2$d4 <- with(dat2, ifelse((d1==0 & d2==0 & d3==0), yes=0, no=d4)) > > dat2ID d1 d2 d3 d4 1 A 0 25 35 25 2 B 12 22 0 12 3 C 0 0 31 31 4 E 10 20 30 10 5 F 0 0 0 0> > table(!is.na(dat2$d4))TRUE 5>Your particular conditionals don't appear sensitive to order, but someone else using the same strategy may have to take care to run the ifelse() statements in the correct (desired) order. HTH, Bill. W. Michels, Ph.D. On Tue, Nov 26, 2019 at 3:15 PM Val <valkremk at gmail.com> wrote:> > HI All, I am having a little issue in my ifelse statement, > The data frame looks like as follow. > > dat2 <-read.table(text="ID d1 d2 d3 > A 0 25 35 > B 12 22 0 > C 0 0 31 > E 10 20 30 > F 0 0 0",header=TRUE,stringsAsFactors=F) > I want to create d4 and set the value based on the following conditions. > If d1 !=0 then d4=d1 > if d1 = 0 and d2 !=0 then d4=d2 > if (d1 and d2 = 0) and d3 !=0 then d4=d3 > if all d1, d2 and d3 =0 then d4=0 > > Here is the desired output and my attempt > ID d1 d2 d3 d4 > A 0 25 35 25 > B 12 22 0 12 > C 0 0 31 31 > E 10 20 30 10 > F 0 0 0 0 0 > > My attempt > dat2$d4 <- 0 > dat2$d4 <- ifelse((dat2$d1 =="0"), dat2$d2, ifelse(dat2$d2 == "0"), dat2$d3, 0) > but not working. > > Thank you. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.