Hi. Yes it is possible.
Here is one approach:

DF <- read.table(textConnection("
Unit  Day    Hour Price Flag
afd1    1/2/2003        1       1       N
afd1    1/2/2003        1       2       N
afd1    1/2/2003        1       3       N
afd1    1/2/2003        1       4       Y
dcf1    1/2/2003        2       2       N
dcf1    1/2/2003        2       3       Y
dcf1    1/2/2003        2       1       N
dcf1    1/2/2003        2       2       N
dcf1    1/2/2003        2       3       Y
ghg2    1/2/2003        3       1       N
afd1    1/2/2003        3       2       N
"),header=TRUE)

cbind(DF, flag = ave(DF$Price, DF$Hour, FUN=function(x) ifelse(x==max(x),
1, 0)))

   Unit      Day Hour Price Flag flag
1  afd1 1/2/2003    1     1    N    0
2  afd1 1/2/2003    1     2    N    0
3  afd1 1/2/2003    1     3    N    0
4  afd1 1/2/2003    1     4    Y    1
5  dcf1 1/2/2003    2     2    N    0
6  dcf1 1/2/2003    2     3    Y    1
7  dcf1 1/2/2003    2     1    N    0
8  dcf1 1/2/2003    2     2    N    0
9  dcf1 1/2/2003    2     3    Y    1
10 ghg2 1/2/2003    3     1    N    0
11 afd1 1/2/2003    3     2    N    1


On Thu, Jun 7, 2012 at 4:17 PM, jcrosbie <ja...@crosb.ie> wrote:

> For a given hour I want to be able to  add a new column called flag.  The
> flag column will flag the highest price in a given hour.  Is there a way to
> do this without a loop?
>
> matrix:
> Unit,   Day,    Hour,   Price,  Flag
> afd1    1/2/2003        1       1       N
> afd1    1/2/2003        1       2       N
> afd1    1/2/2003        1       3       N
> afd1    1/2/2003        1       4       Y
> dcf1    1/2/2003        2       2       N
> dcf1    1/2/2003        2       3       Y
> dcf1    1/2/2003        2       1       N
> dcf1    1/2/2003        2       2       N
> dcf1    1/2/2003        2       3       Y
> ghg2    1/2/2003        3       1       N
> afd1    1/2/2003        3       2       N
> .....
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/flagging-values-without-a-loop-tp4632689.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.
>

        [[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.

Reply via email to