[R] if statement problem

Tim Calkins tim.calkins at gmail.com
Tue Jan 1 23:32:39 CET 2008


you could try the following:

>das$danger <- 0
>das$danger[das$bmi > 30 & das$age > 65] <- 1



On Jan 2, 2008 9:16 AM, Gerard Smits <g_smits at verizon.net> wrote:
>
> Thanks, but I tried the single ampersand, but got a warning msg with
> the same lack of correct assignment:
>
>  > if (das$age>65 & das$bmi>30) {das$danger<-1} else das$danger<-0
> Warning message:
> In if (das$age > 65 & das$bmi > 30) { :
>    the condition has length > 1 and only the first element will be used
>  > attach(das)
>
>          The following object(s) are masked from das ( position 3 ) :
>
>           age bmi day id male sex status
>
>  > das
>
>        id age sex  bmi status day male danger
> 1  33001  35   M 27.5      0 365    1      0
> 2  33002  29   M 34.9      1  22    1      0
> 3  33003  41   F 23.6      0 365    0      0
> 4  33004  55   F 27.0      0 365    0      0
> 5  42001  37   M 39.0      0 365    1      0
> 6  42002  53   M 26.6      1 124    1      0
> 7  42003  46   F 45.4      1 287    0      0
> 8  42004  35   F 36.2      0 365    0      0
> 9  42005  38   F 24.6      0 365    0      0
> 10 42006  58   F 28.0      0 365    0      0
> 11 42007  27   M 25.0      0 365    1      0
> 12 42008  65   F 24.6      0 365    0      0
> 13 42009  25   F 28.0      0 365    0      0
> 14 43001  66   M 27.8      0 365    1      0
> 15 43002  57   F 34.0      0 365    0      0
> 16 43003  45   F 38.1      0 365    0      0
> 17 43004  33   F 53.3      1  62    0      0
> 18 43005  56   F 36.5      0 365    0      0
> 19 43006  31   F 22.4      1   1    0      0
> 20 43007  53   F 32.2      1  21    0      0
> 21 55001  51   M 29.2      0 365    1      0
> 22 55002  33   F 18.7      0 365    0      0
> 23 55003  40   F 30.3      0 365    0      0
> 24 55004  67   M 31.9      0 365    1      0 <-
> 25 55005  41   F 35.0      0 365    0      0
> 26 55006  44   F 37.3      0 365    0      0
> 27 55007  67   M 28.4      1   1    1      0
> 28 55008  65   F 28.8      0 365    0      0
> 29 55009  76   M 18.8      1 225    1      0
> 30 55010  75   F 21.1      1  39    0      0
> 31 63001  30   F 24.9      0 365    0      0
> 32 63002  36   F 47.2      1 377    0      0
> 33 63003  45   F 32.0      0 365    0      0
> 34 63004  49   F 32.3      0 365    0      0
> 35 63005  41   F 20.2      0 365    0      0
> 36 63006  60   F 28.2      0 365    0      0
> 37 63007  33   F 24.5      0 365    0      0
> 38 63008  36   F 28.4      1  56    0      0
> 39 63009  31   F 22.1      0 365    0      0
> 40 63010  77   M 26.6      1   9    1      0
> 41 63011  41   F 32.0      0 365    0      0
> 42 63012  40   F 38.5      1  92    0      0
> 43 63013  27   M 20.6      0 365    1      0
>
>
>
>
> At 02:11 PM 1/1/2008, Christos Hatzis wrote:
> >You need to use '&' instead of '&&':
> >
> >A shorter version of your code using ifelse:
> >
> >das$danger <- with(das, ifelse(age>65 & bmi>30, 1, 0))
> >
> >HTH
> >
> >-Christos
> >
> > > -----Original Message-----
> > > From: r-help-bounces at r-project.org
> > > [mailto:r-help-bounces at r-project.org] On Behalf Of Gerard Smits
> > > Sent: Tuesday, January 01, 2008 5:04 PM
> > > To: r-help at r-project.org
> > > Subject: [R] if statement problem
> > >
> > > Hi All,
> > >
> > > I have a small dataset named das  (43 cases) in which I am
> > > trying to create a binary outcome (1/0) based on the following code:
> > >
> > > if (das$age>65 && das$bmi>30) {das$danger<-1} else das$danger<-0
> > >
> > > I am setting a flag called 'danger' to 1 of the subject is
> > > over 65 and has a BMI > 30.
> > >
> > > I find that my statement evaluates the first record in the
> > > data.frame and then carries that assignment for all. I
> > > detected this as I played around with the values and found
> > > that the T/F status of the first record was always carried
> > > dowqn. I have gotten this to work with an elseif
> > > construction, but would like to know what is happening here.
> > >
> > > Thanks,
> > >
> > > Gerard
> > >
> > > Using: Windows Vista and R 2.61
> > >
> > >
> > > Code and output:
> > >
> > > das<- sasxport.get("c:\\personal\\r\\das.xpt")
> > > if (das$age>65 && das$bmi>30) {das$danger<-1} else das$danger<-0
> > > attach(das)
> > > das
> > >
> > >
> > >          The following object(s) are masked from das ( position 3 ) :
> > >
> > >           age bmi day id male sex status
> > >
> > >        id age sex  bmi status day male danger
> > > 1  33001  35   M 27.5      0 365    1      0
> > > 2  33002  29   M 34.9      1  22    1      0
> > > 3  33003  41   F 23.6      0 365    0      0
> > > 4  33004  55   F 27.0      0 365    0      0
> > > 5  42001  37   M 39.0      0 365    1      0
> > > 6  42002  53   M 26.6      1 124    1      0
> > > 7  42003  46   F 45.4      1 287    0      0
> > > 8  42004  35   F 36.2      0 365    0      0
> > > 9  42005  38   F 24.6      0 365    0      0
> > > 10 42006  58   F 28.0      0 365    0      0
> > > 11 42007  27   M 25.0      0 365    1      0
> > > 12 42008  65   F 24.6      0 365    0      0
> > > 13 42009  25   F 28.0      0 365    0      0
> > > 14 43001  66   M 27.8      0 365    1      0
> > > 15 43002  57   F 34.0      0 365    0      0
> > > 16 43003  45   F 38.1      0 365    0      0
> > > 17 43004  33   F 53.3      1  62    0      0
> > > 18 43005  56   F 36.5      0 365    0      0
> > > 19 43006  31   F 22.4      1   1    0      0
> > > 20 43007  53   F 32.2      1  21    0      0
> > > 21 55001  51   M 29.2      0 365    1      0
> > > 22 55002  33   F 18.7      0 365    0      0
> > > 23 55003  40   F 30.3      0 365    0      0
> > > 24 55004  67   M 31.9      0 365    1      0 <- Problem case should
> > > =1 for danger
> > > 25 55005  41   F 35.0      0 365    0      0
> > > 26 55006  44   F 37.3      0 365    0      0
> > > 27 55007  67   M 28.4      1   1    1      0
> > > 28 55008  65   F 28.8      0 365    0      0
> > > 29 55009  76   M 18.8      1 225    1      0
> > > 30 55010  75   F 21.1      1  39    0      0
> > > 31 63001  30   F 24.9      0 365    0      0
> > > 32 63002  36   F 47.2      1 377    0      0
> > > 33 63003  45   F 32.0      0 365    0      0
> > > 34 63004  49   F 32.3      0 365    0      0
> > > 35 63005  41   F 20.2      0 365    0      0
> > > 36 63006  60   F 28.2      0 365    0      0
> > > 37 63007  33   F 24.5      0 365    0      0
> > > 38 63008  36   F 28.4      1  56    0      0
> > > 39 63009  31   F 22.1      0 365    0      0
> > > 40 63010  77   M 26.6      1   9    1      0
> > > 41 63011  41   F 32.0      0 365    0      0
> > > 42 63012  40   F 38.5      1  92    0      0
> > > 43 63013  27   M 20.6      0 365    1      0
> > >
> > >       [[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.
> > >
> > >
>
>         [[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.
>



-- 
Tim Calkins
0406 753 997




More information about the R-help mailing list