1.What is the 5th element in the original list of ages?
data1 <- data.table::fread("C:/R-language/BACS/customers.txt")
data1[5]
## age
## 1: 45
2.What is the fifth lowest age?
library(dplyr)
##
## 載入套件:'dplyr'
## 下列物件被遮斷自 'package:stats':
##
## filter, lag
## 下列物件被遮斷自 'package:base':
##
## intersect, setdiff, setequal, union
data1[order(data1$age)][5]
## age
## 1: 19
4.Get the five highest ages by first sorting them in decreasing
order first.
head(data1[order(-data1$age)],5)
## age
## 1: 85
## 2: 83
## 3: 82
## 4: 82
## 5: 81
5.What is the average (mean) age?
mean(data1$age)
## [1] 46.80702
6.What is the standard deviation of ages?
sd(data1$age)
## [1] 16.3698
7.Make a new variable called age_diff, with the difference between
each age and the mean age.
age_diff <-abs(data1$age - mean(data1$age))
head(age_diff,5)
## [1] 2.192982 22.192982 5.807018 26.192982 1.807018
8.What is the average “difference between each age and the mean
age”?
mean(age_diff)
## [1] 12.66948
9.Visualize the raw data as we did in class: (a) histogram, (b)
density plot, (c) boxplot+stripchart
hist(data1$age)

plot(data1$age, pch = 8,main = "density plot")

boxplot(data1$age,horizontal = TRUE, main = "boxplot + stripchart")
stripchart(data1$age,add = TRUE,method="jitter",col=2, pch=19,at =1.37)
