close

/*
平均数、排除数值、数列中有NA值、中位数。
Mean、Trim、NA Option、Median。
*/

userName <- c("Lam Wei Wei", "Zheng Da Shi", "Lin Da You", "Fei Gu Man", "Chen Kuang", "Wong wei yun")
salary <- c(8500, 9800, 12500, 15000, 8700, 7500)
jobPosition <- c("Staff", "Manger", "BOSS", "CEO", "Staff", "Staff")
bonus <- c(2300, 1350, 3285, 1035, 3285, 1035)

1. Mean 平均数
MixInfo = c(salary+bonus)
result.mean <- mean(MixInfo)
print(result.mean)

Output result:
12381.67

 

2. Trim 不要某些数值
MixInfo = c(salary+bonus)
result.mean <-  mean(MixInfo,trim = 0.2)
# 不要数列中的首尾各2个
result.mean

Output result:
12430

 

3. NA Option 数列中有NA值
salary <- c(8500, 9800, 12500, 15000, 8700, 7500, NA)
bonus <- c(2300, 1350, 3285, 1035, 3285, 1035, NA)
MixInfo = c(salary+bonus)
result.mean <- mean(MixInfo, na.rm = TRUE)
print(result.mean)

Output result:
12381.67

 

4. Median 中位数
MixInfo = c(salary+bonus)
result.median <- median(MixInfo)
print(result.median)

Output result:
11567.5

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 kk匡 的頭像
    kk匡

    kk匡的日記&學習筆記

    kk匡 發表在 痞客邦 留言(0) 人氣()