1.What is the 5th element in the original list of ages?
exam = read.table(file='customers.txt',header = TRUE)
ages=exam$age
ages[5]
## [1] 45
2.What is the fifth lowest age?
sort_age1=sort(ages)
sort_age1[5]
## [1] 19
3.Extract the five lowest ages together
sort_age1[c(1:5)]
## [1] 18 19 19 19 19
4.Get the five highest ages by first sorting them in decreasing order first.
sort_age2=sort(ages,decreasing= TRUE)
sort_age2[c(1:5)]
## [1] 85 83 82 82 81
5.What is the average (mean) age?
mean(ages)
## [1] 46.80702
6.What is the standard deviation of ages?
sd(ages)
## [1] 16.3698
7.Make a new variable called age_diff, with the difference between each age and the mean age
age_diff = abs(ages-mean(ages))
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
hist(ages)
plot(density(ages))
boxplot(ages, horizontal = TRUE)
stripchart(ages, method = "stack", add = TRUE)