値を持つ data.frame の複数の列 (最初の列を除く) の平均と標準偏差を計算しようとしていNA
ます。
colMeans
、などを試みましたがsapply
、data.frame を実行するループを作成し、平均と標準偏差を別のテーブルに保存しますが、「FUN」エラーが発生し続けます。どんな助けでも素晴らしいでしょう。ありがとう
a
値を持つ data.frame の複数の列 (最初の列を除く) の平均と標準偏差を計算しようとしていNA
ます。
colMeans
、などを試みましたがsapply
、data.frame を実行するループを作成し、平均と標準偏差を別のテーブルに保存しますが、「FUN」エラーが発生し続けます。どんな助けでも素晴らしいでしょう。ありがとう
a
sapply(df, function(cl) list(means=mean(cl,na.rm=TRUE), sds=sd(cl,na.rm=TRUE)))
col1 col2 col3 col4 col5
means 3 8 12.5 18.25 22.5
sds 1.581139 1.581139 1.290994 1.707825 1.290994
as.data.frame( t(sapply(df, function(cl) list(means=mean(cl,na.rm=TRUE),
sds=sd(cl,na.rm=TRUE))) ))
means sds
col1 3 1.581139
col2 8 1.581139
col3 12.5 1.290994
col4 18.25 1.707825
col5 22.5 1.290994
使用する必要がある関数 (例: colMeans
) には、ほとんどすべて、na.rm
デフォルトでと呼ばれるパラメーターがありますFALSE
。実行するだけcolMeans(x = your_df, na.rm = TRUE)
で準備完了です。mean()
列ごとに移動したい場合にのみ使用するのと同じです。
次のコード例は役に立つかもしれません。
# Create a 5 column dataframe that contains some NAs
col1 <- c(1,2,3,4,5)
col2 <- c(6,7,8,9,10)
col3 <- c(11,12,13,14,NA)
col4 <- c(16,NA,18,19,20)
col5 <- c(21,22,23,24,NA)
dataframe <- data.frame(col1,col2,col3,col4,col5)
# Apply the mean() function to all but the first column of the dataframe
apply(dataframe[,2:ncol(dataframe)], 2, function(x) mean(x, na.rm=TRUE))
# Check that the returned values are correct:
mean(col2)
mean(col3, na.rm=TRUE)
mean(col4, na.rm=TRUE)
mean(col5, na.rm=TRUE)
標準偏差は に置き換えmean()
ますsd()
。