[R] Question: Beginner stuck in a R cycle

From: Gustaf Rydevik <gustaf.rydevik_at_gmail.com>
Date: Tue, 08 Jul 2008 18:12:21 +0200

On Tue, Jul 8, 2008 at 3:18 PM, Daniela Ottaviani <d.ottaviani_at_yahoo.it> wrote:
> Dear All,
>
> I have a database of 200 observations named myD.
> In the dataframe there are a column named code (with codes varying from 1 to 77), a column named "prevalence" with some quantitative measurements are given and an column named Pr_mean, with no values.
>
> I would like to set a cycle to compute the average of prevalence values for each different code and store the averages under the empty field Pr_mean.
>
> This is what I wrote:
>
> # Set a cycle
> for (i in 1:nrow(myD)) {
> mycode = myD$code[i]
> mymean[i] = mean(prevalence)
> myD$Pr_mean[i] = mymean[i]
> }
>
> With the above cycle I am able to compute the average of all 200 observations which is then written in every cell.
> I understand that a condition is missing, that indicates that the average has to be computed amongst the observations showing the same codes values.
>
> Could you please help me ?
>
>
> D.
>
>

The easiest thing to do is to use ?by:

myD<-data.frame(code=sample(letters[1:5],200,replace=T),value=rnorm(200)) by(myD$value,myD$code,mean)

but that won't get you the the group means in the empty column without some more lines of code. Another way is to use ?lapply and ?unlist:

myD$Pr_mean<-unlist(lapply(as.character(myD$code),function(x) mean(myD$value[myD$code==x])))

Regards,

Gustaf

--
Gustaf Rydevik, M.Sci.
tel: +46(0)703 051 451
address:Essingetorget 40,112 66 Stockholm, SE
skype:gustaf_rydevik



-- 
Gustaf Rydevik, M.Sci.
tel: +46(0)703 051 451
address:Essingetorget 40,112 66 Stockholm, SE
skype:gustaf_rydevik

______________________________________________
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.
Received on Tue 08 Jul 2008 - 16:17:12 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 08 Jul 2008 - 16:32:05 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive