Re: [R] if statement problem
On 24.12.2011 12:03, reena wrote: It didn't work. :( What did not work??? Please do not misuse the R-help mailing list! Its posting guide clearly asks you to cite the thread and specify reproducible examples that make other able to help. Best, Uwe Ligges -- View this message in context: http://r.789695.n4.nabble.com/if-statement-problem-tp4230026p4230933.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. __ R-help@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.
Re: [R] if statement problem
Hello again. I don't understand what didn't work. First, it seems better to use 'nrow', the result is the same stopifnot(length(x[,1]) == nrow(x)) Then your multiple OR condition. #if((x[i,1] || x[i,2] || x[i,3] || x[i,4]) 5) x - matrix(1:24, ncol=4) for(i in 1:nrow(x)) if(any(x[i,] 5)) cat(At least one is TRUE:, i, \n) for(i in 1:nrow(x)) if((x[i,1] 5) | (x[i,2] 5) | (x[i,3] 5) | (x[i,4] 5)) cat(The same with '|':, i, \n) In the second example each condition IS a condition. Unlike your original compound one. The 'Introduction to R' clearly states '|' as the union of the logical expressions, The section is 2.4. Also read Patrick's PDF, the parts I've read are great and very usefull. (Thanks, Patrick, I was unaware of R inferno.pdf) As a side note, you are duplicating the matrix 'a' assignement. Why not just before the 'if'? Then, you could simply test if any 'a' is less than 5. for (i in 1: row(x)) { a[1,1] - x[i,1]; a[1,2] - x[i,2]; a[2,1] - x[i,3]; a[2,2] - x[i,4]; if(any(a 5)) { etc... (Or use the compound '|' ). Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/if-statement-problem-tp4230026p4233312.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] if statement problem
This is almost Circle 8.1.7 of 'The R Inferno': http://www.burns-stat.com/pages/Tutor/R_inferno.pdf but is making the mistake in the other direction. On 23/12/2011 22:40, reena wrote: Hello, I want to do fisher test for the rows in data file which has value less than 5 otherwise chi square test .The p values from both test should be stored in one resulted file. but there is some problem with bold if statement. I don't know how implement this line properly. x = cbind(obs1,obs2,exp1,exp2) a = matrix(c(0,0,0,0), ncol=2, byrow =TRUE)#matrix with initialized values for (i in 1: length(x[,1])) { *if((x[i,1] || x[i,2] || x[i,3] || x[i,4]) 5)* { a[1,1]- x[i,1]; a[1,2]- x[i,2]; a[2,1]- x[i,3]; a[2,2]- x[i,4]; result- fisher.test(a) write.table(result[[p.value]],file=results.txt, sep=\n, append=TRUE, col.names=FALSE, row.names=FALSE); } else { a[1,1]- x[i,1]; a[1,2]- x[i,2]; a[2,1]- x[i,3]; a[2,2]- x[i,4]; result- chisq.test(a) write.table(result[[p.value]],file=results.txt, sep=\n, append=TRUE, col.names=FALSE, row.names=FALSE);} } Regards R -- View this message in context: http://r.789695.n4.nabble.com/if-statement-problem-tp4230026p4230026.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@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.
Re: [R] if statement problem
It didn't work. :( -- View this message in context: http://r.789695.n4.nabble.com/if-statement-problem-tp4230026p4230933.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] if statement problem
Hello, I want to do fisher test for the rows in data file which has value less than 5 otherwise chi square test .The p values from both test should be stored in one resulted file. but there is some problem with bold if statement. I don't know how implement this line properly. x = cbind(obs1,obs2,exp1,exp2) a = matrix(c(0,0,0,0), ncol=2, byrow =TRUE)#matrix with initialized values for (i in 1: length(x[,1])) { *if((x[i,1] || x[i,2] || x[i,3] || x[i,4]) 5)* { a[1,1] - x[i,1]; a[1,2] - x[i,2]; a[2,1] - x[i,3]; a[2,2] - x[i,4]; result - fisher.test(a) write.table(result[[p.value]],file=results.txt, sep=\n, append=TRUE, col.names=FALSE, row.names=FALSE); } else { a[1,1] - x[i,1]; a[1,2] - x[i,2]; a[2,1] - x[i,3]; a[2,2] - x[i,4]; result - chisq.test(a) write.table(result[[p.value]],file=results.txt, sep=\n, append=TRUE, col.names=FALSE, row.names=FALSE);} } Regards R -- View this message in context: http://r.789695.n4.nabble.com/if-statement-problem-tp4230026p4230026.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] if statement problem
reena wrote Hello, I want to do fisher test for the rows in data file which has value less than 5 otherwise chi square test .The p values from both test should be stored in one resulted file. but there is some problem with bold if statement. I don't know how implement this line properly. x = cbind(obs1,obs2,exp1,exp2) a = matrix(c(0,0,0,0), ncol=2, byrow =TRUE)#matrix with initialized values for (i in 1: length(x[,1])) { *if((x[i,1] || x[i,2] || x[i,3] || x[i,4]) 5)* Hello, Try *if(any(x[i,] 5))* Merry Christmas Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/if-statement-problem-tp4230026p4230135.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[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$age65 das$bmi30) {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$age65 das$bmi30) {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 3651 0 2 33002 29 M 34.9 1 221 0 3 33003 41 F 23.6 0 3650 0 4 33004 55 F 27.0 0 3650 0 5 42001 37 M 39.0 0 3651 0 6 42002 53 M 26.6 1 1241 0 7 42003 46 F 45.4 1 2870 0 8 42004 35 F 36.2 0 3650 0 9 42005 38 F 24.6 0 3650 0 10 42006 58 F 28.0 0 3650 0 11 42007 27 M 25.0 0 3651 0 12 42008 65 F 24.6 0 3650 0 13 42009 25 F 28.0 0 3650 0 14 43001 66 M 27.8 0 3651 0 15 43002 57 F 34.0 0 3650 0 16 43003 45 F 38.1 0 3650 0 17 43004 33 F 53.3 1 620 0 18 43005 56 F 36.5 0 3650 0 19 43006 31 F 22.4 1 10 0 20 43007 53 F 32.2 1 210 0 21 55001 51 M 29.2 0 3651 0 22 55002 33 F 18.7 0 3650 0 23 55003 40 F 30.3 0 3650 0 24 55004 67 M 31.9 0 3651 0 - Problem case should =1 for danger 25 55005 41 F 35.0 0 3650 0 26 55006 44 F 37.3 0 3650 0 27 55007 67 M 28.4 1 11 0 28 55008 65 F 28.8 0 3650 0 29 55009 76 M 18.8 1 2251 0 30 55010 75 F 21.1 1 390 0 31 63001 30 F 24.9 0 3650 0 32 63002 36 F 47.2 1 3770 0 33 63003 45 F 32.0 0 3650 0 34 63004 49 F 32.3 0 3650 0 35 63005 41 F 20.2 0 3650 0 36 63006 60 F 28.2 0 3650 0 37 63007 33 F 24.5 0 3650 0 38 63008 36 F 28.4 1 560 0 39 63009 31 F 22.1 0 3650 0 40 63010 77 M 26.6 1 91 0 41 63011 41 F 32.0 0 3650 0 42 63012 40 F 38.5 1 920 0 43 63013 27 M 20.6 0 3651 0 [[alternative HTML version deleted]] __ R-help@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.
Re: [R] if statement problem
You need to use '' instead of '': A shorter version of your code using ifelse: das$danger - with(das, ifelse(age65 bmi30, 1, 0)) HTH -Christos -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Gerard Smits Sent: Tuesday, January 01, 2008 5:04 PM To: r-help@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$age65 das$bmi30) {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$age65 das$bmi30) {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 3651 0 2 33002 29 M 34.9 1 221 0 3 33003 41 F 23.6 0 3650 0 4 33004 55 F 27.0 0 3650 0 5 42001 37 M 39.0 0 3651 0 6 42002 53 M 26.6 1 1241 0 7 42003 46 F 45.4 1 2870 0 8 42004 35 F 36.2 0 3650 0 9 42005 38 F 24.6 0 3650 0 10 42006 58 F 28.0 0 3650 0 11 42007 27 M 25.0 0 3651 0 12 42008 65 F 24.6 0 3650 0 13 42009 25 F 28.0 0 3650 0 14 43001 66 M 27.8 0 3651 0 15 43002 57 F 34.0 0 3650 0 16 43003 45 F 38.1 0 3650 0 17 43004 33 F 53.3 1 620 0 18 43005 56 F 36.5 0 3650 0 19 43006 31 F 22.4 1 10 0 20 43007 53 F 32.2 1 210 0 21 55001 51 M 29.2 0 3651 0 22 55002 33 F 18.7 0 3650 0 23 55003 40 F 30.3 0 3650 0 24 55004 67 M 31.9 0 3651 0 - Problem case should =1 for danger 25 55005 41 F 35.0 0 3650 0 26 55006 44 F 37.3 0 3650 0 27 55007 67 M 28.4 1 11 0 28 55008 65 F 28.8 0 3650 0 29 55009 76 M 18.8 1 2251 0 30 55010 75 F 21.1 1 390 0 31 63001 30 F 24.9 0 3650 0 32 63002 36 F 47.2 1 3770 0 33 63003 45 F 32.0 0 3650 0 34 63004 49 F 32.3 0 3650 0 35 63005 41 F 20.2 0 3650 0 36 63006 60 F 28.2 0 3650 0 37 63007 33 F 24.5 0 3650 0 38 63008 36 F 28.4 1 560 0 39 63009 31 F 22.1 0 3650 0 40 63010 77 M 26.6 1 91 0 41 63011 41 F 32.0 0 3650 0 42 63012 40 F 38.5 1 920 0 43 63013 27 M 20.6 0 3651 0 [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] if statement problem
Thanks, but I tried the single ampersand, but got a warning msg with the same lack of correct assignment: if (das$age65 das$bmi30) {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 3651 0 2 33002 29 M 34.9 1 221 0 3 33003 41 F 23.6 0 3650 0 4 33004 55 F 27.0 0 3650 0 5 42001 37 M 39.0 0 3651 0 6 42002 53 M 26.6 1 1241 0 7 42003 46 F 45.4 1 2870 0 8 42004 35 F 36.2 0 3650 0 9 42005 38 F 24.6 0 3650 0 10 42006 58 F 28.0 0 3650 0 11 42007 27 M 25.0 0 3651 0 12 42008 65 F 24.6 0 3650 0 13 42009 25 F 28.0 0 3650 0 14 43001 66 M 27.8 0 3651 0 15 43002 57 F 34.0 0 3650 0 16 43003 45 F 38.1 0 3650 0 17 43004 33 F 53.3 1 620 0 18 43005 56 F 36.5 0 3650 0 19 43006 31 F 22.4 1 10 0 20 43007 53 F 32.2 1 210 0 21 55001 51 M 29.2 0 3651 0 22 55002 33 F 18.7 0 3650 0 23 55003 40 F 30.3 0 3650 0 24 55004 67 M 31.9 0 3651 0 - 25 55005 41 F 35.0 0 3650 0 26 55006 44 F 37.3 0 3650 0 27 55007 67 M 28.4 1 11 0 28 55008 65 F 28.8 0 3650 0 29 55009 76 M 18.8 1 2251 0 30 55010 75 F 21.1 1 390 0 31 63001 30 F 24.9 0 3650 0 32 63002 36 F 47.2 1 3770 0 33 63003 45 F 32.0 0 3650 0 34 63004 49 F 32.3 0 3650 0 35 63005 41 F 20.2 0 3650 0 36 63006 60 F 28.2 0 3650 0 37 63007 33 F 24.5 0 3650 0 38 63008 36 F 28.4 1 560 0 39 63009 31 F 22.1 0 3650 0 40 63010 77 M 26.6 1 91 0 41 63011 41 F 32.0 0 3650 0 42 63012 40 F 38.5 1 920 0 43 63013 27 M 20.6 0 3651 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(age65 bmi30, 1, 0)) HTH -Christos -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Gerard Smits Sent: Tuesday, January 01, 2008 5:04 PM To: r-help@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$age65 das$bmi30) {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$age65 das$bmi30) {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 3651 0 2 33002 29 M 34.9 1 221 0 3 33003 41 F 23.6 0 3650 0 4 33004 55 F 27.0 0 3650 0 5 42001 37 M 39.0 0 3651 0 6 42002 53 M 26.6 1 1241 0 7 42003 46 F 45.4 1 2870 0 8 42004 35 F 36.2 0 3650 0 9 42005 38 F 24.6 0 3650 0 10 42006 58 F 28.0 0 3650 0 11 42007 27 M 25.0 0 3651 0 12 42008 65 F 24.6 0 3650 0 13 42009 25 F 28.0 0 3650 0 14 43001 66 M 27.8 0 3651 0 15 43002 57 F 34.0 0 3650 0 16 43003 45 F 38.1 0 3650 0 17 43004 33 F 53.3 1 620 0 18 43005 56 F 36.5 0 3650 0 19 43006 31 F 22.4 1 10 0 20 43007 53 F 32.2 1 210 0 21 55001 51 M 29.2 0 3651 0 22 55002 33 F 18.7 0 3650 0 23 55003 40 F 30.3 0 3650 0 24 55004 67 M 31.9 0 3651 0
Re: [R] if statement problem
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$age65 das$bmi30) {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$age65 das$bmi30) {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 3651 0 2 33002 29 M 34.9 1 221 0 3 33003 41 F 23.6 0 3650 0 4 33004 55 F 27.0 0 3650 0 5 42001 37 M 39.0 0 3651 0 6 42002 53 M 26.6 1 1241 0 7 42003 46 F 45.4 1 2870 0 8 42004 35 F 36.2 0 3650 0 9 42005 38 F 24.6 0 3650 0 10 42006 58 F 28.0 0 3650 0 11 42007 27 M 25.0 0 3651 0 12 42008 65 F 24.6 0 3650 0 13 42009 25 F 28.0 0 3650 0 14 43001 66 M 27.8 0 3651 0 15 43002 57 F 34.0 0 3650 0 16 43003 45 F 38.1 0 3650 0 17 43004 33 F 53.3 1 620 0 18 43005 56 F 36.5 0 3650 0 19 43006 31 F 22.4 1 10 0 20 43007 53 F 32.2 1 210 0 21 55001 51 M 29.2 0 3651 0 22 55002 33 F 18.7 0 3650 0 23 55003 40 F 30.3 0 3650 0 24 55004 67 M 31.9 0 3651 0 - Problem case should =1 for danger 25 55005 41 F 35.0 0 3650 0 26 55006 44 F 37.3 0 3650 0 27 55007 67 M 28.4 1 11 0 28 55008 65 F 28.8 0 3650 0 29 55009 76 M 18.8 1 2251 0 30 55010 75 F 21.1 1 390 0 31 63001 30 F 24.9 0 3650 0 32 63002 36 F 47.2 1 3770 0 33 63003 45 F 32.0 0 3650 0 34 63004 49 F 32.3 0 3650 0 35 63005 41 F 20.2 0 3650 0 36 63006 60 F 28.2 0 3650 0 37 63007 33 F 24.5 0 3650 0 38 63008 36 F 28.4 1 560 0 39 63009 31 F 22.1 0 3650 0 40 63010 77 M 26.6 1 91 0 41 63011 41 F 32.0 0 3650 0 42 63012 40 F 38.5 1 920 0 43 63013 27 M 20.6 0 3651 0 [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] if statement problem
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 [EMAIL PROTECTED] wrote: Thanks, but I tried the single ampersand, but got a warning msg with the same lack of correct assignment: if (das$age65 das$bmi30) {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 3651 0 2 33002 29 M 34.9 1 221 0 3 33003 41 F 23.6 0 3650 0 4 33004 55 F 27.0 0 3650 0 5 42001 37 M 39.0 0 3651 0 6 42002 53 M 26.6 1 1241 0 7 42003 46 F 45.4 1 2870 0 8 42004 35 F 36.2 0 3650 0 9 42005 38 F 24.6 0 3650 0 10 42006 58 F 28.0 0 3650 0 11 42007 27 M 25.0 0 3651 0 12 42008 65 F 24.6 0 3650 0 13 42009 25 F 28.0 0 3650 0 14 43001 66 M 27.8 0 3651 0 15 43002 57 F 34.0 0 3650 0 16 43003 45 F 38.1 0 3650 0 17 43004 33 F 53.3 1 620 0 18 43005 56 F 36.5 0 3650 0 19 43006 31 F 22.4 1 10 0 20 43007 53 F 32.2 1 210 0 21 55001 51 M 29.2 0 3651 0 22 55002 33 F 18.7 0 3650 0 23 55003 40 F 30.3 0 3650 0 24 55004 67 M 31.9 0 3651 0 - 25 55005 41 F 35.0 0 3650 0 26 55006 44 F 37.3 0 3650 0 27 55007 67 M 28.4 1 11 0 28 55008 65 F 28.8 0 3650 0 29 55009 76 M 18.8 1 2251 0 30 55010 75 F 21.1 1 390 0 31 63001 30 F 24.9 0 3650 0 32 63002 36 F 47.2 1 3770 0 33 63003 45 F 32.0 0 3650 0 34 63004 49 F 32.3 0 3650 0 35 63005 41 F 20.2 0 3650 0 36 63006 60 F 28.2 0 3650 0 37 63007 33 F 24.5 0 3650 0 38 63008 36 F 28.4 1 560 0 39 63009 31 F 22.1 0 3650 0 40 63010 77 M 26.6 1 91 0 41 63011 41 F 32.0 0 3650 0 42 63012 40 F 38.5 1 920 0 43 63013 27 M 20.6 0 3651 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(age65 bmi30, 1, 0)) HTH -Christos -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Gerard Smits Sent: Tuesday, January 01, 2008 5:04 PM To: r-help@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$age65 das$bmi30) {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$age65 das$bmi30) {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 3651 0 2 33002 29 M 34.9 1 221 0 3 33003 41 F 23.6 0 3650 0 4 33004 55 F 27.0 0 3650 0 5 42001 37 M 39.0 0 3651 0 6 42002 53 M 26.6 1 1241 0 7 42003 46 F 45.4 1 2870 0 8 42004 35 F 36.2 0 3650 0 9 42005 38 F 24.6 0 3650 0 10 42006 58 F 28.0 0 3650 0 11 42007 27 M 25.0 0 3651 0 12 42008 65 F 24.6 0 3650 0 13 42009 25 F 28.0 0 3650 0 14 43001 66 M 27.8 0 3651 0 15 43002 57 F 34.0 0 3650 0 16 43003 45 F 38.1 0 3650 0 17 43004 33 F 53.3 1 620 0 18 43005 56 F 36.5 0 3650 0 19 43006
Re: [R] if statement problem
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$age65 das$bmi30) {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$age65 das$bmi30) {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 3651 0 2 33002 29 M 34.9 1 221 0 3 33003 41 F 23.6 0 3650 0 4 33004 55 F 27.0 0 3650 0 5 42001 37 M 39.0 0 3651 0 6 42002 53 M 26.6 1 1241 0 7 42003 46 F 45.4 1 2870 0 8 42004 35 F 36.2 0 3650 0 9 42005 38 F 24.6 0 3650 0 10 42006 58 F 28.0 0 3650 0 11 42007 27 M 25.0 0 3651 0 12 42008 65 F 24.6 0 3650 0 13 42009 25 F 28.0 0 3650 0 14 43001 66 M 27.8 0 3651 0 15 43002 57 F 34.0 0 3650 0 16 43003 45 F 38.1 0 3650 0 17 43004 33 F 53.3 1 620 0 18 43005 56 F 36.5 0 3650 0 19 43006 31 F 22.4 1 10 0 20 43007 53 F 32.2 1 210 0 21 55001 51 M 29.2 0 3651 0 22 55002 33 F 18.7 0 3650 0 23 55003 40 F 30.3 0 3650 0 24 55004 67 M 31.9 0 3651 0 - Problem case should =1 for danger 25 55005 41 F 35.0 0 3650 0 26 55006 44 F 37.3 0 3650 0 27 55007 67 M 28.4 1 11 0 28 55008 65 F 28.8 0 3650 0 29 55009 76 M 18.8 1 2251 0 30 55010 75 F 21.1 1 390 0 31 63001 30 F 24.9 0 3650 0 32 63002 36 F 47.2 1 3770 0 33 63003 45 F 32.0 0 3650 0 34 63004 49 F 32.3 0 3650 0 35 63005 41 F 20.2 0 3650 0 36 63006 60 F 28.2 0 3650 0 37 63007 33 F 24.5 0 3650 0 38 63008 36 F 28.4 1 560 0 39 63009 31 F 22.1 0 3650 0 40 63010 77 M 26.6 1 91 0 41 63011 41 F 32.0 0 3650 0 42 63012 40 F 38.5 1 920 0 43 63013 27 M 20.6 0 3651 0 [[alternative HTML version deleted]] __ R-help@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@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.