|
Yusuf Sultan Homework 3 |
University of Southern Maine
College of Science, Technology, and Health Department of Mathematics and Statistics |
26 September 2017 |
Homework 2
1. Sequences
Generate the following sequences and matrices
a. 1,3,5,7,…,21.
seq(1, 21, by = 2)
## [1] 1 3 5 7 9 11 13 15 17 19 21
b. 50,47,44,…,14,11.
seq(50, 11, by = -3)
## [1] 50 47 44 41 38 35 32 29 26 23 20 17 14 11
c. 1,2,4,8,…,1024
2^(0:10)
## [1] 1 2 4 8 16 32 64 128 256 512 1024
d.
t(matrix(1:16, 4))
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 5 6 7 8
## [3,] 9 10 11 12
## [4,] 13 14 15 16
Another way
matrix(1:16, 4, byrow = TRUE)
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 5 6 7 8
## [3,] 9 10 11 12
## [4,] 13 14 15 16
=================================================================================================================
2. Sampling
The command sample() performs random sampling; for example, to give a random permutation of the numbers 1 to 10, we could do one of:
> sample(10) [1] 6 10 5 1 7 8 4 9 2 3
> sample(1:10) [1] 5 4 8 6 2 1 9 3 10 7
a. A scientist needs to experiment upon 4 conditions, 5 times each. Generate a vector ( 1, 1, 1, 1, 1, 2, . . . , 4, 4)T of length 20, representing these conditions.
P2_a <- rep(1:4, each = 5)
P2_a
## [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4
b. The scientist wants to do the 20 experiments in a completely random order; use sample() to reorder the elements of the vector from part (a).
P2_b = sample(P2_a)
P2_b
## [1] 4 1 2 3 2 4 3 1 2 1 4 2 4 2 4 1 3 3 3 1
c. The scientist calls the conditions A, B, C and D. How would you return a character vector with entries “A”, “B”, “C”, “D” containing your random permutation?
LETTERS[P2_a]
## [1] "A" "A" "A" "A" "A" "B" "B" "B" "B" "B" "C" "C" "C" "C" "C" "D" "D"
## [18] "D" "D" "D"
===============================================================================================================
3. Matrices and apply()
Remember that a matrix can be created with the command matrix(), and that it fills in by column first:
> B <- matrix(1:12, nrow = 3)
The command apply() allows you to neatly perform an operation on each row (or column) of a matrix. For example, if you want the row-by-row averages of the matrix B, you could use
> apply(B, 1, mean) [1] 5.5 6.5 7.5
or for the column means, use
> apply(B, 2, mean) [1] 2 5 8 11
Create a 10×11 matrix of independent standard normal random variables; call it B.
B <- matrix(rnorm(110), 10, 11)
B
## [,1] [,2] [,3] [,4] [,5]
## [1,] -1.75457638 -0.56424520 0.72088042 -0.8199708 -0.32240212
## [2,] 0.47191804 0.56359912 -0.96318411 1.0005531 -0.46629143
## [3,] 0.12482909 0.74558978 0.66908773 -0.3143581 0.57392795
## [4,] -0.34212168 1.13876578 0.70462742 -1.8404492 1.19840718
## [5,] -0.06059319 0.62768717 1.28028426 -0.7381234 -0.28799078
## [6,] -0.93439347 0.08988571 -0.94952670 0.9410583 -0.03023479
## [7,] -0.66039287 1.32100776 -0.48164714 1.5200002 0.05183027
## [8,] 0.31825340 0.17169030 0.10659909 0.8446044 -0.37984834
## [9,] -0.13100928 -1.07850441 0.08694907 -0.5742823 -1.56715984
## [10,] -3.01320541 1.87370619 0.12508941 0.1575252 0.72284530
## [,6] [,7] [,8] [,9] [,10]
## [1,] 0.40512080 -0.17009400 -1.1307309 0.18991325 -0.2890101
## [2,] 0.03116372 -0.88530376 -1.0348626 -0.30538554 -1.3698178
## [3,] 0.44587266 -0.03160523 0.4487482 0.05854588 -1.6049523
## [4,] -0.49924114 0.42522833 1.4250322 -0.72011769 -1.3352252
## [5,] -0.64655932 0.14958013 0.2910057 -0.61904120 0.5668017
## [6,] 0.28348672 -0.29680670 -1.3525276 0.73106484 -1.0880525
## [7,] 1.29194811 1.70858998 -0.2581482 -2.00655036 -0.1088977
## [8,] 0.86743101 0.67701323 1.2000029 1.55054752 1.1219805
## [9,] -0.28194861 0.08227142 -0.6805218 -0.08649361 0.2383026
## [10,] -1.20406864 -1.45411228 -0.2239076 -0.18777502 -0.5998200
## [,11]
## [1,] 1.36151944
## [2,] -0.14269674
## [3,] -0.09231942
## [4,] -0.23987281
## [5,] -0.74757461
## [6,] -1.40772175
## [7,] -0.24675582
## [8,] 0.29260760
## [9,] 0.53805595
## [10,] 0.08834930
How would you find the maximum entry in each row of B?
apply(B, 1, max)
## [1] 1.3615194 1.0005531 0.7455898 1.4250322 1.2802843 0.9410583 1.7085900
## [8] 1.5505475 0.5380559 1.8737062
Calculate the standard deviation of each column of B (the command you need is sd()).
apply(B, 2, sd)
## [1] 1.0732622 0.8786893 0.7475941 1.0522570 0.7652572 0.7464234 0.8563246
## [8] 0.9627697 0.9337791 0.9137286 0.7370327
Select the last column of B, and call it b. Then remove the last column from the original B. Do this using the function ncol().
b = B[, ncol(B)]
B = B[, -ncol(B)]
b
## [1] 1.36151944 -0.14269674 -0.09231942 -0.23987281 -0.74757461
## [6] -1.40772175 -0.24675582 0.29260760 0.53805595 0.08834930
B
## [,1] [,2] [,3] [,4] [,5]
## [1,] -1.75457638 -0.56424520 0.72088042 -0.8199708 -0.32240212
## [2,] 0.47191804 0.56359912 -0.96318411 1.0005531 -0.46629143
## [3,] 0.12482909 0.74558978 0.66908773 -0.3143581 0.57392795
## [4,] -0.34212168 1.13876578 0.70462742 -1.8404492 1.19840718
## [5,] -0.06059319 0.62768717 1.28028426 -0.7381234 -0.28799078
## [6,] -0.93439347 0.08988571 -0.94952670 0.9410583 -0.03023479
## [7,] -0.66039287 1.32100776 -0.48164714 1.5200002 0.05183027
## [8,] 0.31825340 0.17169030 0.10659909 0.8446044 -0.37984834
## [9,] -0.13100928 -1.07850441 0.08694907 -0.5742823 -1.56715984
## [10,] -3.01320541 1.87370619 0.12508941 0.1575252 0.72284530
## [,6] [,7] [,8] [,9] [,10]
## [1,] 0.40512080 -0.17009400 -1.1307309 0.18991325 -0.2890101
## [2,] 0.03116372 -0.88530376 -1.0348626 -0.30538554 -1.3698178
## [3,] 0.44587266 -0.03160523 0.4487482 0.05854588 -1.6049523
## [4,] -0.49924114 0.42522833 1.4250322 -0.72011769 -1.3352252
## [5,] -0.64655932 0.14958013 0.2910057 -0.61904120 0.5668017
## [6,] 0.28348672 -0.29680670 -1.3525276 0.73106484 -1.0880525
## [7,] 1.29194811 1.70858998 -0.2581482 -2.00655036 -0.1088977
## [8,] 0.86743101 0.67701323 1.2000029 1.55054752 1.1219805
## [9,] -0.28194861 0.08227142 -0.6805218 -0.08649361 0.2383026
## [10,] -1.20406864 -1.45411228 -0.2239076 -0.18777502 -0.5998200
Solve the system of linear equations Bx= b .
x <- solve(B) %*% b
# or
x <- solve(B, b)
x
## [1] -0.5485759 -0.3851883 -0.6126282 -0.9205529 -0.6745317 2.4550488
## [7] -2.2223125 1.2121836 -1.0150185 0.5473142
Find a vector containing the sums of each row of B. Can you think of (or find) any other ways of achieving this?
apply(B, 1, sum)
## [1] -3.7351150 -2.9576113 1.1156856 0.1549060 0.5630511 -2.6060462
## [7] 2.3777400 6.4782741 -3.9923967 -3.8037228
# Can you think of (or find) any other ways of achieving this?
rowSums(B)
## [1] -3.7351150 -2.9576113 1.1156856 0.1549060 0.5630511 -2.6060462
## [7] 2.3777400 6.4782741 -3.9923967 -3.8037228
B %*% rep(1, ncol(B))
## [,1]
## [1,] -3.7351150
## [2,] -2.9576113
## [3,] 1.1156856
## [4,] 0.1549060
## [5,] 0.5630511
## [6,] -2.6060462
## [7,] 2.3777400
## [8,] 6.4782741
## [9,] -3.9923967
## [10,] -3.8037228
(g) * Create a second matrix A, where the ith column of A is the sum of the first i columns of B.
A = t(apply(B, 1, cumsum))
A
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -1.75457638 -2.3188216 -1.59794116 -2.4179120 -2.7403141 -2.3351933
## [2,] 0.47191804 1.0355172 0.07233305 1.0728861 0.6065947 0.6377584
## [3,] 0.12482909 0.8704189 1.53950661 1.2251485 1.7990765 2.2449491
## [4,] -0.34212168 0.7966441 1.50127151 -0.3391777 0.8592295 0.3599883
## [5,] -0.06059319 0.5670940 1.84737824 1.1092549 0.8212641 0.1747048
## [6,] -0.93439347 -0.8445078 -1.79403446 -0.8529762 -0.8832110 -0.5997243
## [7,] -0.66039287 0.6606149 0.17896775 1.6989679 1.7507982 3.0427463
## [8,] 0.31825340 0.4899437 0.59654279 1.4411472 1.0612989 1.9287299
## [9,] -0.13100928 -1.2095137 -1.12256462 -1.6968469 -3.2640068 -3.5459554
## [10,] -3.01320541 -1.1394992 -1.01440981 -0.8568847 -0.1340394 -1.3381080
## [,7] [,8] [,9] [,10]
## [1,] -2.5052873 -3.6360182 -3.446104941 -3.7351150
## [2,] -0.2475454 -1.2824080 -1.587793518 -2.9576113
## [3,] 2.2133439 2.6620921 2.720637947 1.1156856
## [4,] 0.7852167 2.2102488 1.490131138 0.1549060
## [5,] 0.3242849 0.6152906 -0.003750597 0.5630511
## [6,] -0.8965310 -2.2490586 -1.517993774 -2.6060462
## [7,] 4.7513363 4.4931880 2.486637682 2.3777400
## [8,] 2.6057431 3.8057460 5.356293563 6.4782741
## [9,] -3.4636840 -4.1442057 -4.230699333 -3.9923967
## [10,] -2.7922203 -3.0161279 -3.203902877 -3.8037228
=============================================================================================================
4. Diagonals
a. Create a diagonal matrix whose diagonal entries are 1, 1/2, 1/3,…, 1/10. Call it D.
D <- diag(1/(1:10))
D
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
## [1,] 1 0.0 0.0000000 0.00 0.0 0.0000000 0.0000000 0.000 0.0000000
## [2,] 0 0.5 0.0000000 0.00 0.0 0.0000000 0.0000000 0.000 0.0000000
## [3,] 0 0.0 0.3333333 0.00 0.0 0.0000000 0.0000000 0.000 0.0000000
## [4,] 0 0.0 0.0000000 0.25 0.0 0.0000000 0.0000000 0.000 0.0000000
## [5,] 0 0.0 0.0000000 0.00 0.2 0.0000000 0.0000000 0.000 0.0000000
## [6,] 0 0.0 0.0000000 0.00 0.0 0.1666667 0.0000000 0.000 0.0000000
## [7,] 0 0.0 0.0000000 0.00 0.0 0.0000000 0.1428571 0.000 0.0000000
## [8,] 0 0.0 0.0000000 0.00 0.0 0.0000000 0.0000000 0.125 0.0000000
## [9,] 0 0.0 0.0000000 0.00 0.0 0.0000000 0.0000000 0.000 0.1111111
## [10,] 0 0.0 0.0000000 0.00 0.0 0.0000000 0.0000000 0.000 0.0000000
## [,10]
## [1,] 0.0
## [2,] 0.0
## [3,] 0.0
## [4,] 0.0
## [5,] 0.0
## [6,] 0.0
## [7,] 0.0
## [8,] 0.0
## [9,] 0.0
## [10,] 0.1
b. Now define a 10 × 10 matrix whose entries are all −1 , except on the diagonal, where the entries should be 4. Call it U
U = 5 * diag(10) - 1
U
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 4 -1 -1 -1 -1 -1 -1 -1 -1 -1
## [2,] -1 4 -1 -1 -1 -1 -1 -1 -1 -1
## [3,] -1 -1 4 -1 -1 -1 -1 -1 -1 -1
## [4,] -1 -1 -1 4 -1 -1 -1 -1 -1 -1
## [5,] -1 -1 -1 -1 4 -1 -1 -1 -1 -1
## [6,] -1 -1 -1 -1 -1 4 -1 -1 -1 -1
## [7,] -1 -1 -1 -1 -1 -1 4 -1 -1 -1
## [8,] -1 -1 -1 -1 -1 -1 -1 4 -1 -1
## [9,] -1 -1 -1 -1 -1 -1 -1 -1 4 -1
## [10,] -1 -1 -1 -1 -1 -1 -1 -1 -1 4
c. What is the length of the first column vector in U?
Renormalize the entries of U so that each column is a unit vector. Check directly that your approach is correct.sqrt(sum(U[, 1]^2))
## [1] 5
#Renormalize the entries of U so that each column is a unit vector
U = U/sqrt(sum(U[, 1]^2))
U
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 0.8 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2
## [2,] -0.2 0.8 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2
## [3,] -0.2 -0.2 0.8 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2
## [4,] -0.2 -0.2 -0.2 0.8 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2
## [5,] -0.2 -0.2 -0.2 -0.2 0.8 -0.2 -0.2 -0.2 -0.2 -0.2
## [6,] -0.2 -0.2 -0.2 -0.2 -0.2 0.8 -0.2 -0.2 -0.2 -0.2
## [7,] -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 0.8 -0.2 -0.2 -0.2
## [8,] -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 0.8 -0.2 -0.2
## [9,] -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 0.8 -0.2
## [10,] -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 0.8
# Check directly that your approach is correct.
apply(U, 2, function(x) sqrt(sum(x^2)))
## [1] 1 1 1 1 1 1 1 1 1 1
10 * apply(U, 2, var)
## [1] 1 1 1 1 1 1 1 1 1 1
d. Calculate the matrix UDUT , and call it X.
X = U %*% D %*% U
X
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.7171587 -0.182841270 -0.1495079365 -0.1328412698 -0.12284127
## [2,] -0.1828413 0.417158730 -0.0495079365 -0.0328412698 -0.02284127
## [3,] -0.1495079 -0.049507937 0.3171587302 0.0004920635 0.01049206
## [4,] -0.1328413 -0.032841270 0.0004920635 0.2671587302 0.02715873
## [5,] -0.1228413 -0.022841270 0.0104920635 0.0271587302 0.23715873
## [6,] -0.1161746 -0.016174603 0.0171587302 0.0338253968 0.04382540
## [7,] -0.1114127 -0.011412698 0.0219206349 0.0385873016 0.04858730
## [8,] -0.1078413 -0.007841270 0.0254920635 0.0421587302 0.05215873
## [9,] -0.1050635 -0.005063492 0.0282698413 0.0449365079 0.05493651
## [10,] -0.1028413 -0.002841270 0.0304920635 0.0471587302 0.05715873
## [,6] [,7] [,8] [,9] [,10]
## [1,] -0.11617460 -0.11141270 -0.10784127 -0.105063492 -0.10284127
## [2,] -0.01617460 -0.01141270 -0.00784127 -0.005063492 -0.00284127
## [3,] 0.01715873 0.02192063 0.02549206 0.028269841 0.03049206
## [4,] 0.03382540 0.03858730 0.04215873 0.044936508 0.04715873
## [5,] 0.04382540 0.04858730 0.05215873 0.054936508 0.05715873
## [6,] 0.21715873 0.05525397 0.05882540 0.061603175 0.06382540
## [7,] 0.05525397 0.20287302 0.06358730 0.066365079 0.06858730
## [8,] 0.05882540 0.06358730 0.19215873 0.069936508 0.07215873
## [9,] 0.06160317 0.06636508 0.06993651 0.183825397 0.07493651
## [10,] 0.06382540 0.06858730 0.07215873 0.074936508 0.17715873
e. Find the eigenvalues of X numerically (in R try typing ??eigenvalue). Is this what you expected?
eigen(X)
## eigen() decomposition
## $values
## [1] 1.0000000 0.5000000 0.3333333 0.2500000 0.2000000 0.1666667 0.1428571
## [8] 0.1250000 0.1111111 0.1000000
##
## $vectors
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 0.8 -0.2 -0.2 -0.2 -0.2 0.2 -0.2 0.2 0.2 -0.2
## [2,] -0.2 0.8 -0.2 -0.2 -0.2 0.2 -0.2 0.2 0.2 -0.2
## [3,] -0.2 -0.2 0.8 -0.2 -0.2 0.2 -0.2 0.2 0.2 -0.2
## [4,] -0.2 -0.2 -0.2 0.8 -0.2 0.2 -0.2 0.2 0.2 -0.2
## [5,] -0.2 -0.2 -0.2 -0.2 0.8 0.2 -0.2 0.2 0.2 -0.2
## [6,] -0.2 -0.2 -0.2 -0.2 -0.2 -0.8 -0.2 0.2 0.2 -0.2
## [7,] -0.2 -0.2 -0.2 -0.2 -0.2 0.2 0.8 0.2 0.2 -0.2
## [8,] -0.2 -0.2 -0.2 -0.2 -0.2 0.2 -0.2 -0.8 0.2 -0.2
## [9,] -0.2 -0.2 -0.2 -0.2 -0.2 0.2 -0.2 0.2 -0.8 -0.2
## [10,] -0.2 -0.2 -0.2 -0.2 -0.2 0.2 -0.2 0.2 0.2 0.8
f. Can you use vector recycling to calculate DUT without using matrix multiplication?
c(1/(1:10)) * t(U)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.80000000 -0.20000000 -0.20000000 -0.20000000 -0.20000000
## [2,] -0.10000000 0.40000000 -0.10000000 -0.10000000 -0.10000000
## [3,] -0.06666667 -0.06666667 0.26666667 -0.06666667 -0.06666667
## [4,] -0.05000000 -0.05000000 -0.05000000 0.20000000 -0.05000000
## [5,] -0.04000000 -0.04000000 -0.04000000 -0.04000000 0.16000000
## [6,] -0.03333333 -0.03333333 -0.03333333 -0.03333333 -0.03333333
## [7,] -0.02857143 -0.02857143 -0.02857143 -0.02857143 -0.02857143
## [8,] -0.02500000 -0.02500000 -0.02500000 -0.02500000 -0.02500000
## [9,] -0.02222222 -0.02222222 -0.02222222 -0.02222222 -0.02222222
## [10,] -0.02000000 -0.02000000 -0.02000000 -0.02000000 -0.02000000
## [,6] [,7] [,8] [,9] [,10]
## [1,] -0.20000000 -0.20000000 -0.20000000 -0.20000000 -0.20000000
## [2,] -0.10000000 -0.10000000 -0.10000000 -0.10000000 -0.10000000
## [3,] -0.06666667 -0.06666667 -0.06666667 -0.06666667 -0.06666667
## [4,] -0.05000000 -0.05000000 -0.05000000 -0.05000000 -0.05000000
## [5,] -0.04000000 -0.04000000 -0.04000000 -0.04000000 -0.04000000
## [6,] 0.13333333 -0.03333333 -0.03333333 -0.03333333 -0.03333333
## [7,] -0.02857143 0.11428571 -0.02857143 -0.02857143 -0.02857143
## [8,] -0.02500000 -0.02500000 0.10000000 -0.02500000 -0.02500000
## [9,] -0.02222222 -0.02222222 -0.02222222 0.08888889 -0.02222222
## [10,] -0.02000000 -0.02000000 -0.02000000 -0.02000000 0.08000000
5. Vectors
a. Create a vector of length 12.
vector <-c(3,5,6,2,1,7,8,9,23,11,18,30)
vector
## [1] 3 5 6 2 1 7 8 9 23 11 18 30
length(vector)
## [1] 12
b. Print out the 3rd, 7th, 1st, and 9th elements of the vector (in that order) with one command.
vector[c(3,7,1,9)]
## [1] 6 8 3 23
c. Create a vector of length 32 starting with the value 3.4 and ending with the value 9.6.
vector1 <- seq(3.4,9.6,length=32)
vector1
## [1] 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0 5.2 5.4 5.6 5.8 6.0 6.2 6.4 6.6
## [18] 6.8 7.0 7.2 7.4 7.6 7.8 8.0 8.2 8.4 8.6 8.8 9.0 9.2 9.4 9.6
d. What position is the value 8.0?
which(vector1==8.0)
## [1] 24
e. Create the following vector without the use of the c() command: 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5
sort(rep(seq(1,5),4))
## [1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5
===========================================================================================================
6. Create the following 4 by 4 matrix.
Do the following, each with one command line.
> m2<-matrix(c(1,6,3,2,3,2,7,1,4,12,8,8,2,9,9,0),4,4)
> m2 [,1] [,2] [,3] [,4] [1,] 1 3 4 2 [2,] 6 2 12 9 [3,] 3 7 8 9 [4,] 2 1 8 0
a. Return the 2nd column.
m2<-matrix(c(1,6,3,2,3,2,7,1,4,12,8,8,2,9,9,0),4,4)
m2
## [,1] [,2] [,3] [,4]
## [1,] 1 3 4 2
## [2,] 6 2 12 9
## [3,] 3 7 8 9
## [4,] 2 1 8 0
m2[,2]
## [1] 3 2 7 1
b. Return the element in the 3rd row, 4th column.
m2[3,4]
## [1] 9
c. Change the element in the 2nd row, 2nd column to a 3.
m2[2,2]<-3
m2
## [,1] [,2] [,3] [,4]
## [1,] 1 3 4 2
## [2,] 6 3 12 9
## [3,] 3 7 8 9
## [4,] 2 1 8 0
d. Return the elements on the diagonal.
diag(m2)
## [1] 1 3 8 0
========================================================================================================
7. Do help(rnorm).
rnorm is a function that simulates data from a normal (Gaussian) distribution with a chosen mean and a standard deviation.
a. Create a random sample from a normal distribution of length 200 with mean 4 and standard deviation 2
x<-rnorm(200,4,2)
x
## [1] 3.91702191 1.81610566 1.96413753 5.03298487 3.53619144
## [6] 0.33081222 1.60445861 5.20103052 7.18233278 0.15790658
## [11] 4.91735340 2.71923109 4.47834378 1.68226669 6.81479524
## [16] 3.20311440 3.44826464 7.12146813 6.14973406 6.30916990
## [21] 2.58978040 4.21112826 2.76399283 3.97613045 4.20868032
## [26] 1.35531278 8.10125212 8.28549725 5.22708488 2.38314675
## [31] 7.27860927 1.66796218 4.29593071 5.86188240 3.82571352
## [36] 3.34070146 3.14424329 5.70415436 2.56104485 4.22423967
## [41] 0.43252417 0.29143316 4.00757034 1.89982489 5.03000173
## [46] 4.66427823 5.60246466 0.75500265 6.55665192 1.37158661
## [51] 3.19618175 4.85877840 1.88932831 3.72735049 5.33774826
## [56] 4.83271272 4.51834675 1.41503898 2.44736849 5.96953629
## [61] 4.73383782 3.90325056 3.33934474 4.01538869 4.88605823
## [66] -0.18079070 6.26340893 1.84509284 5.53697732 4.65589116
## [71] 5.10976853 4.90753482 2.53449965 5.61903664 6.60092335
## [76] 0.05009381 2.77567236 3.91510140 3.51847303 4.24384130
## [81] 4.13500311 -0.53820172 4.93492762 9.09341788 2.57386398
## [86] 5.65945376 2.28311595 4.09486852 3.24448792 1.41271584
## [91] 4.36032371 7.03235985 1.91755490 7.36509897 3.38139454
## [96] 4.39367276 2.38982338 3.62565541 0.29901155 2.55531289
## [101] 2.82492414 5.57616958 3.98443911 3.61008039 2.08782721
## [106] 4.70019993 3.62203892 6.72476651 3.68303911 6.56839387
## [111] 8.40565385 4.36637267 5.28596500 1.27459501 2.96421954
## [116] 4.93643964 5.06649238 3.07079994 5.30309995 3.26861176
## [121] 6.61129507 6.36468332 2.16320359 7.16917019 9.38827756
## [126] -0.62590907 4.49239767 3.02983192 5.50210073 5.10317705
## [131] 2.23059560 3.53415273 5.75687328 3.92782215 5.49174447
## [136] 6.40446783 7.77942113 4.75574925 3.51538947 4.97451356
## [141] 7.43727111 5.92280906 0.62802158 2.12389830 1.01636265
## [146] 3.60044894 4.38968818 6.35564497 1.79216031 5.07888967
## [151] 6.47890621 4.68503353 1.36408823 4.45663091 5.29369080
## [156] 3.39147052 4.01652006 5.06791815 3.14828914 3.91019720
## [161] 5.61744893 4.46821164 2.38551916 3.11817270 3.70570771
## [166] 4.90235219 7.62281623 4.66994181 2.45517781 3.36796307
## [171] 4.00872853 2.69512603 4.83883396 5.53965680 2.91664790
## [176] 6.37979666 5.55607788 4.87322621 4.63280987 4.43164430
## [181] 3.86733613 2.78449304 3.15210393 1.06719177 3.67729847
## [186] 5.84053949 4.53456700 5.21678683 4.05252163 4.76849412
## [191] 4.91976724 4.47055102 4.50868256 1.56797215 5.17459409
## [196] 5.21661474 7.66807209 3.77926233 3.96558881 4.76449532
b. How many are less than 2?
sum(x<2)
## [1] 30
c. How many are between 3 and 5?
sum(x>3&x<5)
## [1] 86
d. Calculate the mean and standard deviation of your sample.
mean(x)
## [1] 4.11059
sd(x)
## [1] 1.919099
=========================================================================
8. Read (import) the hw2example.dat into R.
a. Read this table into R.
Data<-read.table("/Users/yusufsultan/Downloads/hw2example.dat")
Data
## X1 X2 X3 X4 X5
## 1 3.33153424 1.004398 3 5.894764 0
## 2 0.74592136 1.923868 3 5.505580 1
## 3 4.17372464 1.706009 3 5.639135 1
## 4 3.43436591 1.098579 NA 4.508899 1
## 5 3.40637181 1.451969 4 5.199390 1
## 6 3.72759064 1.058451 4 5.449157 0
## 7 2.86066006 1.879612 3 3.428353 1
## 8 4.63117152 1.980369 4 5.539757 1
## 9 3.32358225 1.156121 3 4.045489 0
## 10 3.74633404 1.361918 3 6.228370 0
## 11 4.36566275 1.545299 3 4.919844 0
## 12 3.31342316 1.738883 4 4.444122 NA
## 13 1.05611045 1.708640 3 4.211501 1
## 14 3.64186874 1.357633 4 5.971587 1
## 15 3.04647304 1.757369 3 4.461666 1
## 16 3.04492470 1.868577 4 5.180255 0
## 17 2.36054979 1.188440 3 6.581813 1
## 18 3.23782760 1.508255 4 4.142336 1
## 19 3.16170056 1.785478 4 5.266962 1
## 20 3.05766754 1.777159 4 4.798979 1
## 21 2.39967602 1.227438 3 6.532897 1
## 22 2.93130427 1.526575 4 5.859669 1
## 23 3.77888005 1.068323 4 3.100711 1
## 24 2.17337913 1.646210 3 5.070501 1
## 25 2.65455318 1.118435 4 4.800512 0
## 26 5.15896312 1.456361 4 7.341198 1
## 27 4.14781751 1.471349 4 4.869109 1
## 28 2.09779936 1.830025 4 4.302068 1
## 29 3.48758893 1.787008 4 5.024228 1
## 30 4.69525670 1.081643 3 3.733753 0
## 31 3.34730416 1.319182 3 4.988599 1
## 32 3.12393045 1.031023 4 4.701954 0
## 33 3.81391115 1.496336 4 5.628174 0
## 34 2.82510384 1.142516 3 2.378475 1
## 35 2.34574629 1.246614 3 4.628821 1
## 36 3.63693645 1.031583 4 2.826880 1
## 37 4.81078474 1.767520 4 5.124309 0
## 38 2.32380011 1.031423 3 5.403727 0
## 39 4.08864868 1.801791 3 5.036369 1
## 40 2.24756479 1.494370 4 5.226769 0
## 41 1.52255135 1.915535 3 4.115709 1
## 42 2.28038697 1.359145 3 5.554730 1
## 43 2.76975945 1.994715 4 5.821580 0
## 44 2.47673038 1.548286 3 4.194858 0
## 45 1.45359711 1.705938 4 4.983334 1
## 46 1.77877144 1.538249 4 4.947613 0
## 47 3.61281019 1.550545 4 6.334750 1
## 48 2.34615675 1.530629 4 7.929153 0
## 49 2.51199299 1.679296 4 4.286513 1
## 50 2.12918630 1.080149 4 5.487472 0
## 51 3.45631327 1.590754 4 3.846543 0
## 52 3.41202878 1.147021 3 4.889135 0
## 53 2.87381679 1.918666 4 5.037105 1
## 54 1.48587418 1.632598 3 2.500560 1
## 55 3.34949416 1.435319 4 5.089643 0
## 56 3.19159646 1.796000 3 6.045185 0
## 57 2.85309930 1.832351 3 5.443461 1
## 58 4.16823572 1.492039 3 5.723657 0
## 59 3.69150303 1.395315 4 5.281984 1
## 60 3.23190936 1.254583 4 4.222313 1
## 61 2.10263596 1.824777 3 4.920174 1
## 62 0.74326033 1.569586 3 5.611267 1
## 63 3.20855261 1.287598 3 4.226063 1
## 64 3.12392379 1.441511 4 4.616722 1
## 65 2.89732639 1.756547 3 5.555220 1
## 66 2.52296316 1.081077 3 4.649687 1
## 67 3.15643727 1.625364 3 4.094708 1
## 68 3.90345716 1.704591 4 4.968554 1
## 69 3.98097717 1.538652 4 6.204361 0
## 70 0.99131563 1.870734 3 6.741364 0
## 71 4.16509292 1.763945 3 2.604710 1
## 72 3.03583680 1.375602 4 3.641406 0
## 73 1.46461205 1.375915 4 4.026229 0
## 74 1.25289304 1.258808 4 5.538688 0
## 75 3.89646335 1.589964 3 5.840468 1
## 76 3.96208430 1.613451 4 3.533562 0
## 77 3.60235363 1.346602 4 3.516111 0
## 78 3.16045298 1.309424 3 4.581302 1
## 79 5.26012741 1.104514 4 5.974416 1
## 80 0.90894750 1.588400 4 4.554673 1
## 81 2.61263794 1.723709 3 2.536878 1
## 82 3.03723836 1.205281 4 4.278892 1
## 83 3.73756427 1.973892 3 4.088258 0
## 84 2.93912601 1.209670 3 5.230770 0
## 85 1.33195626 1.014907 3 3.991187 0
## 86 2.75518888 1.008302 3 5.488192 0
## 87 3.07212699 1.748181 3 4.815240 1
## 88 4.91105989 1.768290 3 5.731713 0
## 89 2.81131902 1.706517 4 4.810343 1
## 90 4.95839643 1.897388 3 5.193845 1
## 91 2.15160509 1.242871 4 4.986542 0
## 92 3.51862818 1.887370 4 4.938106 1
## 93 3.04799107 1.418100 3 6.050833 0
## 94 2.80315378 1.924678 3 5.328692 0
## 95 4.37057822 1.339338 3 6.442034 0
## 96 1.77059941 1.384112 3 5.416845 0
## 97 3.60522962 1.009867 4 4.924054 0
## 98 3.11497411 NA 4 4.556482 0
## 99 4.32071082 1.676050 4 5.578911 1
## 100 2.16382189 1.616066 3 4.243657 1
## 101 1.71859702 1.600339 3 3.596870 1
## 102 3.09822182 1.413172 4 5.010824 0
## 103 3.60877358 1.204454 4 3.736994 1
## 104 4.88925850 1.686092 4 5.015947 0
## 105 2.34058841 1.707053 3 5.295498 0
## 106 3.24031418 1.919823 3 4.772874 0
## 107 3.07617860 1.655288 3 4.904645 1
## 108 2.90374877 1.088904 4 5.509158 1
## 109 2.55755998 1.502386 3 5.356265 0
## 110 2.61239591 1.249636 3 4.666502 0
## 111 2.50808285 1.747834 4 4.168461 0
## 112 2.72926422 1.983643 3 4.383827 0
## 113 3.07018009 1.839409 4 5.391009 1
## 114 2.48902985 1.330237 4 5.310100 0
## 115 4.62856384 1.927686 4 4.122174 1
## 116 4.08723929 1.214305 4 5.883340 1
## 117 2.71626599 1.150187 4 5.972660 1
## 118 2.52145324 1.974690 4 5.084902 0
## 119 2.06348151 1.506027 4 6.880640 1
## 120 3.57900484 1.083239 3 7.041195 1
## 121 3.19799835 1.678099 4 5.441878 1
## 122 4.18065159 1.270211 4 3.732560 1
## 123 2.28921360 1.105108 3 5.222508 0
## 124 2.33401931 1.309247 3 6.882267 0
## 125 2.38380256 1.492717 4 3.683441 0
## 126 4.24280826 1.511130 4 NA 0
## 127 2.69394856 1.308788 4 3.594316 0
## 128 3.99989905 1.246913 3 6.528875 1
## 129 1.33940803 1.728383 3 4.290297 1
## 130 4.04648831 1.020485 4 3.244062 0
## 131 3.52872035 1.425234 4 5.218773 1
## 132 3.19721156 1.318713 3 6.547400 1
## 133 2.45207935 1.685197 3 3.766856 0
## 134 2.67418649 1.104325 3 5.730242 1
## 135 3.61290532 1.053881 3 3.039416 1
## 136 1.31474998 1.793558 3 6.067525 0
## 137 3.59926871 1.528531 3 6.111114 0
## 138 2.74034546 1.583933 4 3.122225 1
## 139 3.20024283 1.104305 3 4.672150 0
## 140 2.61987808 1.635036 4 4.598037 0
## 141 2.26347266 1.078154 4 5.429991 1
## 142 4.48978364 1.763461 4 4.306174 0
## 143 3.98021461 1.469863 3 3.178803 0
## 144 3.98118536 1.989636 3 5.793931 1
## 145 3.69423630 1.439159 3 4.384322 1
## 146 3.74503182 1.113358 3 5.287515 1
## 147 2.29259092 1.759066 3 4.206645 1
## 148 3.84132759 1.696584 3 5.911489 1
## 149 2.19909805 1.430019 3 6.291624 0
## 150 1.19009465 1.011350 3 5.511643 1
## 151 1.96480391 1.436664 3 6.975333 0
## 152 2.57919455 1.420495 3 5.040426 0
## 153 3.63538095 1.498944 4 4.212466 1
## 154 1.76039566 1.200464 4 5.314172 1
## 155 -0.09600634 1.653146 4 4.382514 0
## 156 3.22351493 1.011157 3 5.186961 1
## 157 3.38717668 1.118788 4 3.789369 0
## 158 2.68832997 1.836223 4 6.216372 0
## 159 3.02843321 1.676124 4 4.661311 1
## 160 4.34856653 1.929030 3 4.867723 1
## 161 3.26143848 1.571119 4 5.477713 1
## 162 2.00497334 1.461343 4 6.327508 0
## 163 1.97580224 1.792737 3 4.661614 1
## 164 2.03026551 1.766999 3 6.715426 1
## 165 3.27771779 1.736170 4 4.829996 0
## 166 3.81707724 1.792394 4 4.437433 1
## 167 3.42654160 1.591000 3 4.773865 0
## 168 3.40413769 1.479366 3 4.635559 0
## 169 3.94630280 1.913967 4 2.663087 1
## 170 2.88824284 1.358996 4 2.471850 1
## 171 2.61056159 1.391233 4 5.158222 1
## 172 4.39396002 1.367760 3 4.269230 0
## 173 4.06952308 1.812201 4 4.629297 0
## 174 1.24857934 1.322102 3 4.860743 0
## 175 0.07085426 1.960223 4 4.428968 0
## 176 2.52700308 1.364251 3 2.912591 0
## 177 5.54232808 1.950472 4 4.611855 0
## 178 1.94795690 1.279724 3 4.680629 0
## 179 2.87321735 1.155133 3 5.879619 1
## 180 3.89694695 1.876132 3 4.112814 1
## 181 2.32746861 1.144932 3 5.585528 0
## 182 2.72238235 1.369698 3 6.156892 0
## 183 3.53040001 1.944420 4 5.080295 1
## 184 3.84234230 1.319159 4 5.022152 0
## 185 1.99553983 1.812800 4 4.188579 0
## 186 3.02168402 1.816535 3 4.252498 0
## 187 3.16624231 1.137334 3 3.974728 0
## 188 4.28273841 1.636097 3 4.341650 0
## 189 3.00915833 1.182063 3 5.597612 1
## 190 4.17585529 1.448073 3 4.208136 1
## 191 4.32799787 1.547021 4 6.645748 1
## 192 1.81650979 1.873637 3 5.601434 0
## 193 1.49190019 1.052198 3 6.498690 0
## 194 2.94574470 1.069560 3 7.298965 0
## 195 4.38123008 1.272599 4 5.431848 1
## 196 1.88157367 1.976833 3 3.211451 0
## 197 2.67214403 1.682132 4 4.050776 0
## 198 3.96372311 1.817453 3 3.248944 1
## 199 3.96763961 1.791894 3 3.418702 0
## 200 4.75985883 1.633238 4 4.881594 0
## 201 1.54179006 1.880902 3 5.802577 0
## 202 1.82263794 1.780872 3 3.941481 1
## 203 1.34609936 1.575861 4 4.450133 1
## 204 2.54148020 1.276559 4 4.590593 0
## 205 2.68090152 1.927431 4 5.162133 1
## 206 2.74397595 1.000048 4 6.151712 1
## 207 2.31192923 1.861323 3 4.470612 1
## 208 2.56004945 1.797224 3 3.245195 1
## 209 3.71856142 1.650897 3 5.511899 0
## 210 3.42025733 1.909480 4 3.139193 0
## 211 0.67475996 1.208703 4 4.679578 1
## 212 4.38032268 1.258289 3 6.211830 1
## 213 3.33326171 1.788585 4 5.381816 0
## 214 2.40451638 1.710017 4 5.365836 0
## 215 3.86567951 1.388762 NA 5.072515 1
## 216 2.84724482 1.001732 3 4.745398 0
## 217 3.50289650 1.570406 4 5.936269 1
## 218 4.77215995 1.117192 4 4.208159 1
## 219 2.67152550 1.407123 4 6.194095 1
## 220 3.34967044 1.716052 4 4.265958 1
## 221 3.48482418 1.291658 4 4.885245 0
## 222 3.70022615 1.262960 4 5.532976 1
## 223 3.32562358 1.519453 3 4.863505 1
## 224 2.65393518 1.931870 3 2.091993 0
## 225 2.97603301 1.032148 3 4.682323 0
## 226 1.72315169 1.038745 3 6.484663 0
## 227 2.38204275 1.951822 4 4.647379 0
## 228 3.29944415 1.721590 4 5.366059 1
## 229 3.13685064 1.011882 4 6.535541 0
## 230 2.89155286 1.955469 4 3.718586 1
b. What are its dimensions?
dim(Data)
## [1] 230 5
c. Find the mean, standard deviation, minimum, and maximum of the fourth column.
mean(Data[,4]);sd(Data[,4],na.rm=TRUE);min(Data[,4],na.rm=TRUE);max(Data[,4],na.rm=TRUE)
## [1] NA
## [1] 1.028961
## [1] 2.091993
## [1] 7.929153
# NA (must have some missing data in the column)
d. With one command, return the 1st, 3rd, and 23rd rows.
Data[c(1,3,23),]
## X1 X2 X3 X4 X5
## 1 3.331534 1.004398 3 5.894764 0
## 3 4.173725 1.706009 3 5.639135 1
## 23 3.778880 1.068323 4 3.100711 1
e. With one command, return the 2nd and 4th columns for rows 112 and 113.
Data[112:113,c(2,4)]
## X2 X4
## 112 1.983643 4.383827
## 113 1.839409 5.391009
f. Where are the missing values in the data?
We can approach this by looking at one column at a time. (looking at each row would take longer)
which(is.na(Data[,1]));which(is.na(Data[,2]));which(is.na(Data[,3]));which(is.na(Data[,4]));which(is.na(Data[,5]))
## integer(0)
## [1] 98
## [1] 4 215
## [1] 126
## [1] 12
There are no missing values in column 1, one in column 2, two in column 3, one in column 4, and one in column 5.
[98,2], [4,3], [215,3], [126,4], [12,5]