Frage

I'm a bit stuck with the calculation of standard deviations and would be great if you could give me some help with the 2 QUESTIONS below.

1. My initial data

Day Drink   People
1   Coffee  1
1   Coffee  3
1   Tea 5
1   Tea 4
1   Mint    7
1   Mint    5
2   Coffee  1
2   Coffee  3
2   Tea 4
2   Tea 8
2   Mint    9
2   Mint    3
3   Coffee  2
3   Coffee  4
3   Tea 5
3   Tea 3
3   Mint    2
3   Mint    2

2. Average.

Code & output:

t <- aggregate(People ~ Day + Drink, data=t, mean)
Day Drink   People
1   Coffee  2
1   Tea 5
1   Mint    6
2   Coffee  2
2   Tea 6
2   Mint    6
3   Coffee  3
3   Tea 4
3   Mint    2

QUESTION 1: HOW CAN I CALCULATE THE STANDARD ERROR (STANDARD DEVIATION OF THE MEAN) FOR THIS?

3. Accumulated values.

Code & output:

t <- transform(t, AccPeople = ave(People, Drink, FUN=cumsum))
Day Drink   People  Acc People
1   Coffee  2   2
1   Tea 5   5
1   Mint    6   6
2   Coffee  2   4
2   Tea 6   11
2   Mint    6   12
3   Coffee  3   7
3   Tea 4   15
3   Mint    2   14

QUESTION 2: HOW CAN I CALCULATE THE ACCUMULATED STANDARD DEVIATION FOR THIS?

Thanks a lot!! (and sorry for the bad formatting of the data!)

War es hilfreich?

Lösung

You can use ddply from the plyr package:

library(plyr)
  1. Calculate mean and standard error (and standard deviation):

    dat2 <- ddply(dat, .(Day, Drink), summarise, 
                  M = mean(People), SE = sd(People) / sqrt((length(People))), 
                  SD = sd(People))
    

    where dat is the name of your data frame.

  2. Calculate accumulated mean and accumulated standard deviation:

    ddply(dat2, .(Drink), mutate, 
          Macc = cumsum(M), SDacc = cumsum(SD))
    
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top