Skip to content
Prev 314825 / 398503 Next

Conditional Statistics

You can use the tapply function to do this. You can't type a line into 
the mean statement. (See ?mean for what you can type in there). The 
general approach is to have a vector of data (stock prices) and a 
categorical variable (day of week). Then break up the data vector 
according to the levels in the categorical variable, and calculate the 
mean values:

Weekmeans <- tapply(data.vector, catvariable, mean)

This will give you the means for all days. If you really just want one 
mean (just monday), you could do:

Monmean <- mean(data.vector[catvariable=="Monday"])

Similarly, if you want the standard deviation for each day of the week, 
you would use:

WeekSD <- tapply(data.vector, catvariable, sd)
MonSD <- sd(data.vector[catvariable=="Monday"])

You will find that some things that are easy in SAS require a little 
more thought in R, and vice versa. Certainly, the philosophical approach 
to data analysis in R is different to that in SAS. There are a couple of 
books for R for SAS users. They might help you.

Cheers,

Simon.
On 08/01/13 11:17, Joseph Norman Thomson wrote: