[R] condition apply to elements of a data frame column
arnaud Gaboury
arnaud.gaboury at gmail.com
Wed May 26 15:14:22 CEST 2010
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:jorismeys at gmail.com]
Sent: Wednesday, May 26, 2010 2:48 PM
To: arnaud Gaboury
Cc: r-help at 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.gaboury at 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 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.
--
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.Meys at Ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
More information about the R-help
mailing list