i am trying to calculate some statistical moments, using the moments function, for a group of variables. In order to do this i try to program a function
When I try to use my function with my dataframe that does not have NA or missing, I confirmed that the data is clean, I have the next error:
library(moments)
summary.stat0 = function(data){
SUM = matrix(NA,ncol=ncol(data),nrow=7)
colnames(SUM) = colnames(data)
rownames(SUM) = c("Mean","Median","Maximum","Minimum","Std. Dev.","Skewness","Kurtosis")
for (i in 1:ncol(data)) {
SUM[1,i] = mean(data[,i])
SUM[2,i] = median(data[,i])
SUM[3,i] = max(data[,i])
SUM[4,i] = min(data[,i])
SUM[5,i] = sd(data[,i])
SUM[6,i] = skewness(data[,i])
SUM[7,i] = kurtosis(data[,i])
}
SUM
}
Error in median.default(data[i]) : need numeric data In addition: Warning message: In mean.default(data[, i]) :
If someone can help me with this function i will be grateful
You can compute all needed statistics in one apply
loop.
library(moments)
summary.stat <- function(data, na.rm = TRUE){
SUM <- apply(data, 2, function(x){
c(Mean = mean(x, na.rm = na.rm),
Median = median(x, na.rm = na.rm),
Max = max(x, na.rm = na.rm),
Min = min(x, na.rm = na.rm),
`Std. Dev.` = sd(x, na.rm = na.rm),
Skewness = skewness(x, na.rm = na.rm),
Kurtosis = kurtosis(x, na.rm = na.rm)
)})
colnames(SUM) <- colnames(data)
SUM
}
set.seed(1234)
df1 <- as.data.frame(replicate(3, rnorm(10)))
summary.stat(df1)
# V1 V2 V3
#Mean -0.3831574 -0.1181707 -0.38794682
#Median -0.5555419 -0.4941011 -0.46561688
#Max 1.0844412 2.4158352 0.57475572
#Min -2.3456977 -0.9983864 -1.44820491
#Std. Dev. 0.9957875 1.0673376 0.66600127
#Skewness -0.4243347 1.5011660 0.02058876
#Kurtosis 2.6686992 4.1925245 1.87910667