[R] R syntaxe

2007-02-02 Thread Martin Olivier
Hi all,

Suppose I have a vector x with numerical values.
In y, I have a categorial variable : y<-c(1,1,..2,2,...30,30,30)
x and y have the same length.
I would like to compute the mean for x for the modality 1 to 30 in y.
mean(x[y==1]),...,mean(x[y==30])
I do not want to use an iterative procedure such that for (i in 1:30)..
Thanks for your help,

Regards.
Olivier.


-- 

-
Martin Olivier
INRA - Unité Biostatistique & Processus Spatiaux
Domaine St Paul, Site Agroparc
84914 Avignon Cedex 9, France
Tel : 04 32 72 21 57

__
R-help@stat.math.ethz.ch 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.


Re: [R] R syntaxe

2007-02-02 Thread Roland Rau
Hi,


On 2/2/07, Martin Olivier <[EMAIL PROTECTED]> wrote:
>
> Hi all,
>
> Suppose I have a vector x with numerical values.
> In y, I have a categorial variable : y<-c(1,1,..2,2,...30,30,30)
> x and y have the same length.
> I would like to compute the mean for x for the modality 1 to 30 in y.
> mean(x[y==1]),...,mean(x[y==30])


I hope I understand your question correctly, but is this what you are
looking for?

x <- runif(1)
y <- sample(x=1:30, size=length(x), replace=TRUE)

one.possibility <- tapply(X=x, INDEX=list(my.categ.var=y), FUN=mean)

another.possibility <- by(data=x, INDICES=list(my.categ.var=y), FUN=mean)
one.possibility
another.possibility


Which one you prefer depends probably on your taste and what you intend to
do with the results.

HTH,
Roland

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch 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.