求助这个R语言代码什么意思啊T^T跪谢

> basic.stats <- function(x,more=F) {
+ stats <- list()
+
+ clean.x <- x[!is.na(x)]
+
+ stats$n <- length(x)
+ stats$nNAs <- stats$n-length(clean.x)
+
+ stats$mean <- mean(clean.x)
+ stats$std <- sd(clean.x)
+ stats$med <- median(clean.x)
+ if(more) {
+ stats$skew <- sum(((clean.x-stats$mean)/stats$std)^3)/
+ length(clean.x)
+ stats$kurt <- sum(((clean.x-stats$mean)/stats$std)^4)/
+ length(clean.x) - 3
+ }
+ unlist(stats)
+ }

一行一行来。
basic.stats <- function(x,more=F) { # 建立名叫basic.stats的函数,参数为x和more,more默认是F就是不用输入,但你也可以输入,有额外效果。

stats <- list() #建立名叫stats的列表类型变量

clean.x <- x[!is.na(x)] #把x中的NA全部踢掉,留下有用的数据记为clean.x

stats$mean <- mean(clean.x) # 计算clean.x的均值 赋给列表中的mean单元

stats$std <- sd(clean.x) # 计算clean.x的标准差 赋给列表中的std单元

stats$med <- median(clean.x) # 计算clean.x的中位数 赋给列表中的med单元

if(more) { #如果你在函数中输入2个变量,默认是basic.stats(x),你可以输入basic.stats(x, y) 有额外效果

stats$skew <- sum(((clean.x-stats$mean)/stats$std)^3)/length(clean.x) #计算偏度 赋给列表中的skew单元

stats$kurt <- sum(((clean.x-stats$mean)/stats$std)^4)/length(clean.x) - 3 #计算峰度 赋给列表中的kurt单元

unlist(stats) #最后拆解列表变量stats 使其变为简单的向量数值变量
温馨提示:答案为网友推荐,仅供参考