I used this service several months ago and was very pleased with the response. I have a dataframe with several thousand lines and to each line I need to apply a series of "if else" statements. For each row I need either a value or a blank/NA. Below is the series of if else statements I have been trying without success to integrate into a function such as "apply". if (t1.secondstrongest<-13000) {NA} else if (noise>-13000) {NA} else if ((abs(value.str1-value.str2))==120) {NA} else if ((abs(value.str1-value.str2))==180) {NA} else if ((abs(value.str1-value.str2))==240) {NA} else if (value.str1 ==300&&value.str2 ==0) {value.str1_adj+ bearingdiff} else if (value.str1 ==0&&value.str2 ==360) {value.str1_adj+360 - bearingdiff} else if (value.str2<value.str1) {value.str1_adj-bearingdiff} else if (value.str2>value.str1) {value.str1_adj+bearingdiff} Below is an example of the dataframe t1.secondstrongest Ant_test value.str1 value.str1_adj value.str2 value.str2_adj Noise bearingdiff 1 -10682 60 240 242 180 182 -12344 11.23 2 -11154 240 0 2 240 242 -13444 27.23 3 -10930 300 0 2 300 302 -14022 27.55 4 -10997 240 300 302 60 62 -13456 14.23 5 -11244 180 180 182 0 2 -14209 25.22 6 -12744 60 180 182 240 242 -14134 8.13 The answer to the examples should be... 1 NA 2 NA 3 334.45 4 NA 5 NA 6 190.13 THANKS! Mike Department of Natural Resources and Environmental Sciences University of Illinois [[alternative HTML version deleted]]
Mike, One thing I noticed that '&&' should be replaced by '&'. The former is applied to the first element of the vector while the latter applicable to the whole vector. HTH Weidong Gu On Wed, Aug 10, 2011 at 7:34 PM, Ward, Michael Patrick <mpward at illinois.edu> wrote:> > I used this service several months ago and was very pleased with the response. > > I have a dataframe with several thousand lines and to each line I need to apply a series of "if else" statements. For each row I need either a value or a blank/NA. Below is the series of if else statements I have been trying without success to integrate into a function such as "apply". > > if (t1.secondstrongest<-13000) {NA} else > if (noise>-13000) {NA} else > if ((abs(value.str1-value.str2))==120) {NA} else > if ((abs(value.str1-value.str2))==180) {NA} else > if ((abs(value.str1-value.str2))==240) {NA} else > if (value.str1 ==300&&value.str2 ==0) {value.str1_adj+ bearingdiff} else > if (value.str1 ==0&&value.str2 ==360) {value.str1_adj+360 - bearingdiff} else > if (value.str2<value.str1) {value.str1_adj-bearingdiff} else > if (value.str2>value.str1) {value.str1_adj+bearingdiff} > > > Below is an example of the dataframe > > t1.secondstrongest ? ? ? ? Ant_test ? ? ? ? ? ? ?value.str1 ? ? ? ? ? ?value.str1_adj ? ? ? ? ? ? ? ? ? value.str2 ? ? ? ? ? ?value.str2_adj ? ? ? ? ? ? ? ? ? Noise ? ? ? ? ? ? ? ? ? ?bearingdiff > 1 ? ? ? ? ? ? ?-10682 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?60 ? ? ? ? ? ? ? ? ? ? ? ? ? 240 ? ? ? ? ? ? ? ? ? ? ? ? 242 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 180 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 182 ? ? ? ? ? ? ? ? ? ? ? ? -12344 ? ? ? ? ? ? ? ? ? 11.23 > 2 ? ? ? ? ? ? ?-11154 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?240 ? ? ? ? ? ? ? ? ? ? ? ? 0 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?2 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?240 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 242 ? ? ? ? ? ? ? ? ? ? ? ? -13444 ? ? ? ? ? ? ? ? ? 27.23 > 3 ? ? ? ? ? ? ?-10930 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 300 ? ? ? ? ? ? ? ? ? ? ? ?0 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?2 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?300 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 302 ? ? ? ? ? ? ? ? ? ? ? ? -14022 ? ? ? ? ? ? ? ? ? 27.55 > 4 ? ? ? ? ? ? ?-10997 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 240 ? ? ? ? ? ? ? ? ? ? ? ? 300 ? ? ? ? ? ? ? ? ? ? ? ?302 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 60 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 62 ? ? ? ? ? ? ? ? ? ? ? ? ? -13456 ? ? ? ? ? ? ? ? ? 14.23 > 5 ? ? ? ? ? ? ?-11244 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?180 ? ? ? ? ? ? ? ? ? ? ? ? ?180 ? ? ? ? ? ? ? ? ? ? ? ?182 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?2 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?-14209 ? ? ? ? ? ? ? ? ? 25.22 > 6 ? ? ? ? ? ? ?-12744 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?60 ? ? ? ? ? ? ? ? ? ? ? ? ?180 ? ? ? ? ? ? ? ? ? ? ? ?182 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 240 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 242 ? ? ? ? ? ? ? ? ? ? ? ? -14134 ? ? ? ? ? ? ? ? ? 8.13 > > The answer to the examples should be... > > 1 ? ? ? ? ? ? ? ? ? ? NA > > 2 ? ? ? ? ? ? ? ? ? ? NA > > 3 ? ? ? ? ? ? ? ? ? ? 334.45 > > 4 ? ? ? ? ? ? ? ? ? ? NA > > 5 ? ? ? ? ? ? ? ? ? ? NA > > 6 ? ? ? ? ? ? ? ? ? ? 190.13 > > > THANKS! > Mike > Department of Natural Resources and Environmental Sciences > University of Illinois > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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 Aug 10, 2011, at 7:34 PM, Ward, Michael Patrick wrote:> > I used this service several months ago and was very pleased with the > response. > > I have a dataframe with several thousand lines and to each line I > need to apply a series of "if else" statements. For each row I need > either a value or a blank/NA. Below is the series of if else > statements I have been trying without success to integrate into a > function such as "apply".Try this: dfrm <- structure(list(t1.secondstrongest = c(-10682L, -11154L, -10930L, -10997L, -11244L, -12744L), Ant_test = c(60L, 240L, 300L, 240L, 180L, 60L), value.str1 = c(240L, 0L, 0L, 300L, 180L, 180L), value.str1_adj = c(242L, 2L, 2L, 302L, 182L, 182L), value.str2 = c(180L, 240L, 300L, 60L, 0L, 240L), value.str2_adj = c(182L, 242L, 302L, 62L, 2L, 242L ), Noise = c(-12344L, -13444L, -14022L, -13456L, -14209L, -14134L ), bearingdiff = c(11.23, 27.23, 27.55, 14.23, 25.22, 8.13)), .Names = c("t1.secondstrongest", "Ant_test", "value.str1", "value.str1_adj", "value.str2", "value.str2_adj", "Noise", "bearingdiff"), row.names = c("1", "2", "3", "4", "5", "6"), class = "data.frame") ddfrm$newval <- NA dfrm$newval <-apply(dfrm, 1, function(x) ifelse( any( x["t1.secondstrongest"]< -13000, x["Noise"] > -13000 , abs(x["value.str1"]-x["value.str2"])==120 , abs(x["value.str1"]-x["value.str2"])==180 , abs(x["value.str1"]-x["value.str2"])==240 ), NA, ifelse (x["value.str1"] == 300 & x["value.str2"] ==0, x["value.str1_adj"] + x["bearingdiff"], ifelse (x["value.str1"] == 0 & x["value.str2"] == 360 , x["value.str1_adj"] + 360 - x["bearingdiff"] , ifelse (x["value.str2"] < x["value.str1"] , x["value.str1_adj"] - x["bearingdiff"], ifelse ( x["value.str2"] > x["value.str1"] , x["value.str1_adj"] + x["bearingdiff"], NA ) ) ) ) ) )> > Below is an example of the dataframe > > t1.secondstrongest Ant_test > value.str1 value.str1_adj > value.str2 value.str2_adj > Noise bearingdiff > 1 -10682 > 60 240 > 242 > 180 > 182 -12344 11.23 > 2 -11154 > 240 0 > 2 > 240 > 242 -13444 27.23 > 3 -10930 > 300 0 > 2 > 300 > 302 -14022 27.55 > 4 -10997 > 240 300 > 302 > 60 > 62 -13456 14.23 > 5 -11244 > 180 180 > 182 > 0 > 2 -14209 25.22 > 6 -12744 > 60 180 > 182 > 240 > 242 -14134 8.13 > > The answer to the examples should be... > > 1 NA > 2 NA > 3 334.45 > 4 NA > 5 NA > 6 190.13 > >I get > as.matrix(dfrm$newval) [,1] [1,] NA [2,] NA [3,] 29.55 [4,] NA [5,] NA [6,] 190.13 Given the number of syntactic errors that needed to be corrected, I trust R more than your "hand" calculations. Not jsut the erroneous use of "&&" but in particular your unfortunate practice of not using spaces created a bug where your wrote: t1.secondstrongest<-13000 Notice the warning message: Warning messages: 1: In any(x["t1.secondstrongest"] <- 13000, x["Noise"] > -13000, abs(x["value.str1"] - : coercing argument of type 'double' to logical That attempted to assign a value rather than testing whether a number is less than negative 13000. MORAL: USE SPACES TO IMPROVE READABILITY AND AVOID SYNTACTIC ERRORS.> THANKS! > Mike > Department of Natural Resources and Environmental Sciences > University of IllinoisDavid Winsemius, MD West Hartford, CT
Reasonably Related Threads
- Trouble retrieving the second largest value from each row of a data.frame
- After writing data in MMF using SEXP structure, can i reference in R?
- Looking for help with an ast matcher
- Looking for help with an ast matcher
- [LLVMdev] Adding "S" suffixed ARM/Thumb2 instructions