Hello, I wrote this code which works fine on a single observation: x<-100 y<-200 z<-125 aa<-150 if(x<z && y>z) {aa-z} result: 25 I am trying to apply this logic where x,y,z,and aa are arrays but with very little success. I have tried using loops and whiles but I always get errors of various types. I have consulted a few manuals but with limited success. My hopeful outcome would be: data: X Y Z AA 1 100 200 125 150 2 125 110 105 140 3 110 150 130 200 4 90 200 75 65 Here row 1 would return 25, row 2 would return nothing since Z<X, row 3 would be 50, row 4 would be nothing since X>Z. In this case I am trying to return something where I could call the output a variable "Z" which would become an array based on the logic above where the members would be: (25, 50) I tried using this where the variables are arrays: if(x<z && y>z) {aa-z} But I get "NULL" I tried using this where the variables are arrays: if(x<z && y>z) {aa-z} else "NA" But I get "NA" When I tried using a loop: for(i in 1:length(x)) if(x<z && y>z) {aa-z} else "NA" I got "NULL" An attempt at a "while" statement crashed. The time series resource ( http://cran.r-project.org/doc/contrib/Ricci-refcard-ts.pdf) is way too involved. Most online sources I am finding are not good for "if" contingent array manipulation. Annoyingly, just saying aa-z will work across the entire array but for some reason my ifs fail. I know the R help group only wants serious emails so hopefully this will indicate I gave it a reasonable shot. Please help Thanks [[alternative HTML version deleted]]
Hello George, Two things should help you move from one case to multiple. First, if is designed for a single result (TRUE or FALSE), but you actually want many, so you can use the ifelse() function. Second, since you want to examine each element, you just want '&', not '&&'. I believe this does what you are after: # read in the data # this is just the output of dput() which makes it easy to share data via email dat <- structure(list(X = c(100L, 125L, 110L, 90L), Y = c(200L, 110L, 150L, 200L), Z = c(125L, 105L, 130L, 75L), AA = c(150L, 140L, 200L, 65L)), .Names = c("X", "Y", "Z", "AA"), class = "data.frame", row.names = c(NA, -4L)) # with() makes it so that I do not have to keep referencing 'dat' # which contains each variable with(dat, ifelse(X < Z & Y > Z, AA - Z, NA)) For some documentation on this stuff see: ?ifelse # for working with many elements ?with # to save your fingers from typing ?'&' # note the quotes ?dput # for an easy way to provide data Hope that helps, Josh On Thu, Sep 16, 2010 at 1:51 PM, George Coyle <gcoyle4 at gmail.com> wrote:> Hello, > > I wrote this code which works fine on a single observation: > > x<-100 > y<-200 > z<-125 > aa<-150 > if(x<z && y>z) {aa-z} > result: 25 > > I am trying to apply this logic where x,y,z,and aa are arrays but with very > little success. ?I have tried using loops and whiles but I always get errors > of various types. ?I have consulted a few manuals but with limited success. > My hopeful outcome would be: > > data: > ? ?X ? ? ? Y ? ? ?Z ? ? ?AA > 1 100 ?200 ? ?125 ? ? 150 > 2 125 ? 110 ? ?105 ? ?140 > 3 110 ? ?150 ? 130 ? ?200 > 4 90 ? ?200 ? ? 75 ? ?65 > > Here row 1 would return 25, row 2 would return nothing since Z<X, row 3 > would be 50, row 4 would be nothing since X>Z. > > In this case I am trying to return something where I could call the output a > variable "Z" which would become an array based on the logic above where the > members would be: (25, 50) > > I tried using this where the variables are arrays: > if(x<z && y>z) {aa-z} > But I get "NULL" > > ?I tried using this where the variables are arrays: > if(x<z && y>z) {aa-z} else "NA" > But I get "NA" > > When I tried using a loop: > for(i in 1:length(x)) if(x<z && y>z) {aa-z} else "NA" > I got "NULL" > > An attempt at a "while" statement crashed. > > The time series resource ( > http://cran.r-project.org/doc/contrib/Ricci-refcard-ts.pdf) is way too > involved. ?Most online sources I am finding are not good for "if" contingent > array manipulation. ?Annoyingly, just saying aa-z will work across the > entire array but for some reason my ifs fail. ?I know the R help group only > wants serious emails so hopefully this will indicate I gave it a > reasonable shot. > > Please help > > Thanks > > ? ? ? ?[[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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
George - I think you're looking for the ifelse function:> X = c(100,125,110,90) > Y = c(200,110,150,200) > Z = c(125,105,130,75) > AA = c(150,140,200,65) > result = ifelse(X < Z & Y > Z,AA - Z,NA) > result[1] 25 NA 70 NA> result[!is.na(result)][1] 25 70 (I'm assuming you said 50 when you meant 70.) - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Thu, 16 Sep 2010, George Coyle wrote:> Hello, > > I wrote this code which works fine on a single observation: > > x<-100 > y<-200 > z<-125 > aa<-150 > if(x<z && y>z) {aa-z} > result: 25 > > I am trying to apply this logic where x,y,z,and aa are arrays but with very > little success. I have tried using loops and whiles but I always get errors > of various types. I have consulted a few manuals but with limited success. > My hopeful outcome would be: > > data: > X Y Z AA > 1 100 200 125 150 > 2 125 110 105 140 > 3 110 150 130 200 > 4 90 200 75 65 > > Here row 1 would return 25, row 2 would return nothing since Z<X, row 3 > would be 50, row 4 would be nothing since X>Z. > > In this case I am trying to return something where I could call the output a > variable "Z" which would become an array based on the logic above where the > members would be: (25, 50) > > I tried using this where the variables are arrays: > if(x<z && y>z) {aa-z} > But I get "NULL" > > I tried using this where the variables are arrays: > if(x<z && y>z) {aa-z} else "NA" > But I get "NA" > > When I tried using a loop: > for(i in 1:length(x)) if(x<z && y>z) {aa-z} else "NA" > I got "NULL" > > An attempt at a "while" statement crashed. > > The time series resource ( > http://cran.r-project.org/doc/contrib/Ricci-refcard-ts.pdf) is way too > involved. Most online sources I am finding are not good for "if" contingent > array manipulation. Annoyingly, just saying aa-z will work across the > entire array but for some reason my ifs fail. I know the R help group only > wants serious emails so hopefully this will indicate I gave it a > reasonable shot. > > Please help > > Thanks > > [[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. >
I see Joshua has kindly offered some resources to turn you from your path of erroR and sins against the interpreteR, but if you want to do extra penance, you might consider reading the R-FAQ (although your issue if if and "&&" is surprisingly not included there) : http://cran.r-project.org/doc/FAQ/R-FAQ.html http://cran.r-project.org/doc/FAQ/R-FAQ.html#R-Miscellanea Then depending on the level of further contRition you might want to exert, you could also read the "R Inferno" (Abstract: If you are using R and you think you?re in hell, this is a map for you.) by Patrick Burns: http://www.burns-stat.com/pages/Tutor/R_inferno.pdf (see section 3.2 for your problem) -- David. On Sep 16, 2010, at 4:51 PM, George Coyle wrote:> Hello, > > I wrote this code which works fine on a single observation: > > x<-100 > y<-200 > z<-125 > aa<-150 > if(x<z && y>z) {aa-z} > result: 25 > > I am trying to apply this logic where x,y,z,and aa are arrays but > with very > little success. I have tried using loops and whiles but I always > get errors > of various types. I have consulted a few manuals but with limited > success. > My hopeful outcome would be: > > data: > X Y Z AA > 1 100 200 125 150 > 2 125 110 105 140 > 3 110 150 130 200 > 4 90 200 75 65 > > Here row 1 would return 25, row 2 would return nothing since Z<X, > row 3 > would be 50, row 4 would be nothing since X>Z. > > In this case I am trying to return something where I could call the > output a > variable "Z" which would become an array based on the logic above > where the > members would be: (25, 50) > > I tried using this where the variables are arrays: > if(x<z && y>z) {aa-z} > But I get "NULL" > > I tried using this where the variables are arrays: > if(x<z && y>z) {aa-z} else "NA" > But I get "NA" > > When I tried using a loop: > for(i in 1:length(x)) if(x<z && y>z) {aa-z} else "NA" > I got "NULL" > > An attempt at a "while" statement crashed. > > The time series resource ( > http://cran.r-project.org/doc/contrib/Ricci-refcard-ts.pdf) is way too > involved. Most online sources I am finding are not good for "if" > contingent > array manipulation. Annoyingly, just saying aa-z will work across the > entire array but for some reason my ifs fail. I know the R help > group only > wants serious emails so hopefully this will indicate I gave it a > reasonable shot. > > Please help > > Thanks > > [[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.David Winsemius, MD West Hartford, CT