## Input data frame
df <- data.frame(Name=sample(letters,5),Month=c(1,2,3,1,2),Rate1=c(12,18,19,53,22),Rate2=c(23,73,45,19,87))
## Split data set on month
df_splitted <- split(df[,3:4],df$Month)
## Desired Output
df_out <- data.frame(Month=as.numeric(names(df_splitted)),
Mean=sapply(lapply(df_splitted,unlist),mean),
sd=sapply(lapply(df_splitted,unlist),sd),
stringsAsFactors=FALSE)
## Plot
plot(df_out$Month, df_out$Mean,
ylim=range(c(df_out$Mean-df_out$sd, df_out$Mean+df_out$sd)),
pch=19, xlab="Measurements", ylab="Mean +/- SD",
main="Scatter plot with std.dev error bars")
arrows(df_out$Month, df_out$Mean-df_out$sd, df_out$Month,
df_out$Mean+df_out$sd, length=0.05, angle=90, code=3)
## Explanation
# Split the data frame into a list of data frame while keeping
# rows with same month value together
temp1 <- split(df[,3:4],df$Month)
# Convert the list of data frames into list of vectors
temp2 <- lapply(temp1,unlist)
# For every vector in list it calculates the mean
sapply(temp2,mean)
ベクターの名前は、それが計算される月を表して得られたオブジェクトがベクトルです。
これらの値はどのように 'mean_rate'カラムに入っていますか?あなたの出力を説明できますか? –
これは、両方の列の値を使用して各月の平均値にする必要があります。私は例を含めるために私の質問を更新しました。私は助けることを願っています – CGFoX
ちょうど 'ライブラリ(dplyr); df%>%group_by(月)%>%summaryize(new =平均(Rate1 + Rate2)/ 2) '? – Sotos