2017-10-24 4 views
2

より多くの観測値を追加すると、安定したグループの平均値がどのようになるかを知りたい。R:グループ内のmean/sdを計算する方法。常に行単位で行を1つ追加する

のは、私は次のようなデータを持っているとしましょう:

   email score 
      <chr> <int> 
1 [email protected]  4 
2 [email protected]  3 
3 [email protected]  3 
4 [email protected]  4 
5 [email protected]  1 
6 [email protected]  4 
7 [email protected]  5 
8 [email protected]  5 

次に、([email protected][email protected])2つの異なるグループのために、私は行で平均& SD行を計算したいです、 1行ずつ追加します。 3行目のために - mean(4,3), sd(4,3)mean(4,3,3), sd(4,3,3)のように...だろう、この例の

所望の出力は次のようになります。私はこれを実装するにはどうすればよい

  email score  mean  sd 
      <chr> <int> <dbl>  <dbl> 
1 [email protected]  4 4.000000  NA 
2 [email protected]  3 3.500000 0.7071068 
3 [email protected]  3 3.333333 0.5773503 
4 [email protected]  4 3.500000 0.5773503 
5 [email protected]  1 1.000000  NA 
6 [email protected]  4 2.500000 2.1213203 
7 [email protected]  5 3.333333 2.0816660 
8 [email protected]  5 3.750000 1.8929694 

だから、行2のためにそれがあるべきR? おかげ

答えて

2

これらは観測を注文している場合これはあなた

あなたのデータ

df <- read.table(text="email score 
1 [email protected]  4 
2 [email protected]  3 
3 [email protected]  3 
4 [email protected]  4 
5 [email protected]  1 
6 [email protected]  4 
7 [email protected]  5 
8 [email protected]  5", header=TRUE) 

ソリューション

library(tidyverse) 
df %>% 
    group_by(email) %>% 
    nest(score) %>% 
    mutate(data = map(data, ~map_df(seq_len(nrow(.x)), function(i) tibble(mean = mean(.x$score[1:i]), sd = sd(.x$score[1:i]))))) %>% 
    unnest(data) 

出力

# A tibble: 8 x 3 
      # email  mean  sd 
      # <fctr> <dbl>  <dbl> 
# 1 [email protected] 4.000000  NA 
# 2 [email protected] 3.500000 0.7071068 
# 3 [email protected] 3.333333 0.5773503 
# 4 [email protected] 3.500000 0.5773503 
# 5 [email protected] 1.000000  NA 
# 6 [email protected] 2.500000 2.1213203 
# 7 [email protected] 3.333333 2.0816660 
# 8 [email protected] 3.750000 1.8929694 
+0

fantastic!どうもありがとう! – maxtenzin

+0

ようこそ。 – CPak

0

のために働くかもしれません、グループ変数を使ってrep()し、集約します。あなたは適切なreprexを持っていたが、私はあなたの例で作業しようとするでしょうかどうかは容易になるだろう:

df$group <- rep(a:b, n) # where a:b represents how many observations you have for each unique id and n is how many unique ids are in the dataset // this would be rep(1:4, 2) in your example 

temp1 <- aggregate(df, list(group), FUN=mean) # aggregate to get mean 
temp2 <- aggregate(df, list(group), FUN=sd) # aggregate to get sd 

out <- data.frame(unique(df$email)) 
out <- merge(out, temp1, by.x="email", by.y="email") 
out <- merge(out, temp2, by.x="email", by.y="email") 

これは完璧ではないもそれは特に赤身のですが、ロジックはあなたの問題を解決するためのお手伝いをする必要があります。

関連する問題