Arnaud, check the vector : > u$POSITION<>0 [1] TRUE TRUE ...
what I do is putting u$POSITION==0 [1] FALSE FALSE ... when you apply the sum() function on that vector, FALSE becomes 0 and TRUE becomes 1. So this actually gives you a way of counting the amount of positions that are not zero. if you have one element -2 and another 2, then > c(-2,0,2)==0 [1] FALSE TRUE FALSE >sum(c(-2,0,2)==0) [1] 1 Which gives you exactly the amount of elements that is 0. Cheers Joris On Wed, May 26, 2010 at 3:14 PM, arnaud Gaboury <arnaud.gabo...@gmail.com>wrote: > Joris, > > I want to add a line in a function with a print "warning" if one element > of > the column is <>0. > I could use if(sum(u$POSITION)<>0) as a condition, but I can imagine having > one element equal to -2, and another one to 2. So in this case, sum=0, but > the condition is false in fact (minimum of one element different from > zero). > > > > > > > From: Joris Meys [mailto:jorism...@gmail.com] > Sent: Wednesday, May 26, 2010 2:48 PM > To: arnaud Gaboury > Cc: r-help@r-project.org > Subject: Re: [R] (no subject) > > What exactly are you trying to do? If you want to know which position is > wrong, try : > > if (sum(u$POSITION==0)>0) cat("WARNING:POSITION IS WRONG FOR > ",which(u$POSITION==0),"\n") > > or even : > wrong <- which(u$POSITION==0) > if(length(wrong)>0) cat("WARNING: POSITION IS WRONG > FOR",u$DESCRIPTION[wrong],"\n") > > Gives you the exact location of wrong positions. If you do that, make sure > u$DESCRIPTION is a character vector and not a factor. > > Cheers > Joris > On Wed, May 26, 2010 at 2:31 PM, arnaud Gaboury <arnaud.gabo...@gmail.com> > wrote: > Dear group, > > Here is my data frame: > > > dput(u) > structure(list(DESCRIPTION = structure(c(2L, 5L, 6L, 7L, 9L, > 11L, 12L, 15L, 14L, 16L, 1L, 10L, 3L, 4L, 13L, 8L, 17L), .Label = c("COFFEE > C Jul/10", > "COPPER May/10", "CORN Jul/10", "CORN May/10", "COTTON NO.2 Jul/10", > "CRUDE OIL miNY May/10", "GOLD Jun/10", "HENRY HUB NATURAL GAS May/10", > "ROBUSTA COFFEE (10) Jul/10", "SILVER May/10", "SOYBEANS Jul/10", > "SPCL HIGH GRADE ZINC USD", "STANDARD LEAD USD", "SUGAR NO.11 Jul/10", > "SUGAR NO.11 May/10", "WHEAT Jul/10", "WHEAT May/10"), class = "factor"), > PL = c(3500, -1874.99999999999, -2612.50000000003, -2169.99999999998, > -680, 425, 1025, 1008.00000000000, -3057.59999999999, 3212.5, > -1781.25000000001, -2265.0, 75, -387.5, 2950, 490.000000000013, > 0), POSITION = c(-2, 3, 2, 2, 18, 3, -1, -1, 5, 5, 0, 0, > 0, 0, 0, 0, 0)), .Names = c("DESCRIPTION", "PL", "POSITION" > ), class = "data.frame", row.names = c(NA, -17L)) > > I want to give a warning message if one of the element of the POSITION > column is different from zero. > > I tried using mapply with some line like this : > > > mapply(if,u$POSITION,==0,print("WARNING:POSITIONS ARE WRONG",quote=F)) > But it seems it is not the correct way to pass the various arguments. > > Any help is appreciated > > > > > *************************** > Arnaud Gaboury > Mobile: +41 79 392 79 56 > BBM: 255B488F > > ______________________________________________ > 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. > > > > -- > Joris Meys > Statistical Consultant > > Ghent University > Faculty of Bioscience Engineering > Department of Applied mathematics, biometrics and process control > > Coupure Links 653 > B-9000 Gent > > tel : +32 9 264 59 87 > joris.m...@ugent.be > ------------------------------- > Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php > > -- Joris Meys Statistical Consultant Ghent University Faculty of Bioscience Engineering Department of Applied mathematics, biometrics and process control Coupure Links 653 B-9000 Gent tel : +32 9 264 59 87 joris.m...@ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php [[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.