[R] group by function

Bert Gunter bgunter.4567 at gmail.com
Sat Nov 7 16:48:36 CET 2015


??
 Row 4 has i = 2 and Measure_id =4 and therefore has value divided by
the max of all values with that i and Measure_id, which is 1. Row 7
has i =2 and Measure_id =2, and so is divided by the max value in all
rows with those values of i and Measure_id, which is 2. etc. So either
you do not understand ave() or you are incorrectly stating what you
want.

Incidentally, read ?ave, ?with and ?within. paste() is not needed and
the call can be shortened to

df <- within(df,newcol<-ave(value,i,Measure_id,FUN=function(x)x/max(x)))

Finally, I suggest you learn to post in plain text (as the posting
guide requests) to avoid possible mangling of your posts.

Cheers,
Bert

Bert Gunter

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
   -- Clifford Stoll


On Sat, Nov 7, 2015 at 6:32 AM, Ragia Ibrahim <ragia11 at hotmail.com> wrote:
> Dear group,
> kindly, I have the following data frame
>
> structure(c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2,
> 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1.5, 2, 1, 0, 2, 2, 1.5,
> 0, 0, 1, 1, 2, 0, 1, 2), .Dim = c(15L, 3L), .Dimnames = list(
>     NULL, c("i", "Measure_id", "value")))
>
>
>
> it has 3 coulmns
> I used
>     df$newcoulmn <-  ave(df$value, paste( df$i, df$Measure_id), FUN= function(x)   x/max(x) )
>
>
> to get x /max(x) and calc probability.. but I want to do that for each source measure_id" separately  that means that rows 4,7,13 ..their value should be divided by 1.5 not 2..
>
> rows 1,5,11 will be divided by  2 the max of their same group grouped by measure id
>  how to make such grouping
>
> thanks in advance
> Ragia
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list