Solution 1
v <- c(length(rivers), sum(rivers), mean(rivers), median(rivers), var(rivers),
sd(rivers), min(rivers), max(rivers))
v
## [1] 141.0000 83357.0000 591.1844 425.0000 243908.4086 493.8708
## [7] 135.0000 3710.0000
Solution 2
The trim=0.25
argument means that R will ignore 25 percent of the 8 observations (i.e., 2 observations) from both the lowest and highest end. So, what’s left is the mean of 1, 2, 3 and 6, which is 3.
Part a
mean(c(-100, 0, 1, 2, 3, 6, 50, 73), trim = 0.25)
## [1] 3
Part b
trimfraction <- 10/length(rivers)
mean(rivers, trim = trimfraction)
## [1] 505.719
Solution 3
Part a
attach(women)
cor(height, weight)
## [1] 0.9954948
Part b
cov(height, weight)
## [1] 69
Part c
cor(height, weight, method = "pearson")
## [1] 0.9954948
cor(height, weight, method = "kendall")
## [1] 1
cor(height, weight, method = "spearman")
## [1] 1
The default is method='pearson'
.
Solution 4
Part a
floor
and trunc
. If you experienced trouble with the exercise, a good way to proceed would be to first write out for each element of x
separately, which of the three functions could have been chosen. E.g.:
300.99 -> 300 : trunc
or floor
1.6 -> 1: trunc
or floor
583 -> 583: trunc
or floor
or ceiling
42.10 -> 42: trunc
or floor
Because only trunc
and floor
could have been chosen for all 4 values, only these two could have been chosen for the entire vector as well.
x <- c(300.99, 1.6, 583, 42.1)
floor(x)
## [1] 300 1 583 42
trunc(x)
## [1] 300 1 583 42
Part b
trunc
. Note that contrary to part (a), floor
doesn’t work, because of the negative value (-2.4) in x
.
x <- c(152.34, 1940.63, 1.0001, -2.4, sqrt(26))
trunc(x)
## [1] 152 1940 1 -2 5
Part c
trunc
and ceiling
. Note: all values are negative because of the -
sign before c(...)
.
x <- -c(3.2, 444.35, 1/9, 100)
trunc(x)
## [1] -3 -444 0 -100
ceiling(x)
## [1] -3 -444 0 -100
Solution 5
Part a
round
with d=0
.
x <- c(35.63, 300.2, 0.39, -57.8)
round(x, digits = 0)
## [1] 36 300 0 -58
Part b
signif
with d=3
x <- c(35.63, 300.2, 0.39, -57.8)
signif(x, digits = 3)
## [1] 35.60 300.00 0.39 -57.80
Part c
Both signif
and round
with d=2
x <- c(3.8, 0.983, -23, 7.1)
signif(x, 2)
## [1] 3.80 0.98 -23.00 7.10
round(x, 2)
## [1] 3.80 0.98 -23.00 7.10
Solution 6
The solution for this exercise is available in our eBook Start Here To Learn R – vol. 1: Vectors, arithmetic, and regular sequences.
Solution 7
The solution for this exercise is available in our eBook Start Here To Learn R – vol. 1: Vectors, arithmetic, and regular sequences.
Solution 8
The solution for this exercise is available in our eBook Start Here To Learn R – vol. 1: Vectors, arithmetic, and regular sequences.
Solution 9
The solution for this exercise is available in our eBook Start Here To Learn R – vol. 1: Vectors, arithmetic, and regular sequences.
Solution 10
The solution for this exercise is available in our eBook Start Here To Learn R – vol. 1: Vectors, arithmetic, and regular sequences.
Leave a Reply