Hi Domenico,
I was incorrectly assuming it would use a vector of equal length to
my data. frame. Thanks for the clarification.
Also, thanks for the many alternate programming approaches provided by others.
Gerard
At 02:25 PM 1/1/2008, Domenico Vistocco wrote:
>You should look for your answer using the help for the if statement (?"if").
>The cond argument should be a scalar (otherwise only the first
>element is used).
>
>?"if"
>.....
>cond: A length-one logical vector that is not 'NA'. Conditions of
> length greater than one are accepted with a warning, but only
> the first element is used. Other types are coerced to
> logical if possible, ignoring any class.
>...
>
>The ifelse statement check the condition for each element of your
>input (returning
>a value with the same shape).
>
>domenico
>
>Gerard Smits wrote:
>>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]]
>>
>>______________________________________________
>>[email protected] 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]]
______________________________________________
[email protected] 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.