[R] If statements for multiple arrays

Joshua Wiley jwiley.psych at gmail.com
Fri Sep 17 00:28:43 CEST 2010


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/



More information about the R-help mailing list