1. 程式人生 > >R-描述性統計

R-描述性統計

RT。。。老實說這一章我是抖的。。。但是,加油~

# 從1:100中均勻抽取size個數據,replace=TRUE指有放回抽樣,資料可以重複
x = sample(1:100, size = 100, replace = TRUE)
y = x
# 隨機設定y中有20%的缺失值
y[sample(x = 1:100, size = 20, replace = FALSE)] = NA
# 不設定na.rm=TRUE的話,mean會返回NA,哪怕只有一個缺失值
# mean, weighted.mean, var, sd, min, max, median
mean(y, na.rm = TRUE)
# weighted.mean
weighted.mean(x = y, w = y, na.rm = TRUE)
# min, 1st quantile, median, mean, 3rd quantile, max, #NA
summary(y)
# probs設定想要的分位數,要設定na.rm
quantile(y, probs = c(0.25, 0.5, 0.75), na.rm = TRUE)