Is this what you want:

> w <- c(1.20, 1.34, 2.34, 3.12, 2.89, 4.67, 2.43,
+       2.89, 1.99, 3.45, 2.01, 2.23, 1.45, 1.59)
> g <- rep(c("a", "b"), each=7)
> df <- data.frame(g, w)
> df
   g    w
1  a 1.20
2  a 1.34
3  a 2.34
4  a 3.12
5  a 2.89
6  a 4.67
7  a 2.43
8  b 2.89
9  b 1.99
10 b 3.45
11 b 2.01
12 b 2.23
13 b 1.45
14 b 1.59
> # add coefficient to use
> vari <- c(b=0.08, a=0.41)
> result <- by(df, list(df$g), function(x){
+     # convert $g to character since it is a factor
+     x$w[(x$w > mean(x$w) - vari[as.character(x$g)]) &
+         (x$w < mean(x$w) + (1 - vari[as.character(x$g)]))]
+ })
> result
: a
[1] 2.34 3.12 2.89 2.43
------------------------------------------------------------------------------

: b
[1] 2.89 2.23
>
>

On Sat, May 17, 2008 at 2:04 PM, Patrick Hausmann <
[EMAIL PROTECTED]> wrote:

> Hello all,
>
> I have a df like this:
>
> w <- c(1.20, 1.34, 2.34, 3.12, 2.89, 4.67, 2.43,
>       2.89, 1.99, 3.45, 2.01, 2.23, 1.45, 1.59)
> g <- rep(c("a", "b"), each=7)
> df <- data.frame(g, w)
> df
>
> # 1. Mean for each group
> tapply(df$w, df$g, function(x) mean(x))
>
> # 2. Range for each group - fix value 0.15
> tapply(df$w, df$g,
>       function(x)
>       x[(x > mean(x) - 0.15) &
>         (x < mean(x) + ( 1 - 0.15 ))])
>
> Now my question: How can I use different values of 0.15 for
> each group. As a result of a calculation I have an object
> "vari":
>
>  > vari
>    a    b
>  0.41 0.08
>
>  > str(vari)
>  num [, 1:2] 0.41 0.08
>  - attr(*, "dimnames")=List of 1
>   ..$ : chr [1:2] "a" "b
>
> So, I wanted to use 0.41 for group "a" and 0.08 for "b"
> instead of 0.15...
>
> Thanks for any help!!
> Patrick
>
> ______________________________________________
> 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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

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