MTH-2520 Homework 3: Vectors


Pr. 1a

The character string “3.1415926” can be converted to a number using the as.numeric() function: 3.1415926

#Pr. 1a - Convert to number
as.numeric("3.1415926")
[1] 3.141593

Pr. 1b

Form the vector: seq(1, 99, 3) + c(1, e, \(\pi\)). Round each element to 3 decimal places. Before I begin let me state my plan of attack. I intend to assign this vector to x and then round x to 3 decimal places.

#Pr. 1b - Round the vector

x <- seq(from = 1, to = 99, by = 3) + c(1, exp(1), pi)
round(x, digits = 3)
 [1]   2.000   6.718  10.142  11.000  15.718  19.142  20.000  24.718  28.142
[10]  29.000  33.718  37.142  38.000  42.718  46.142  47.000  51.718  55.142
[19]  56.000  60.718  64.142  65.000  69.718  73.142  74.000  78.718  82.142
[28]  83.000  87.718  91.142  92.000  96.718 100.142
seq(from = 1, to = 99, by = 3)
 [1]  1  4  7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73
[26] 76 79 82 85 88 91 94 97

So, what do we see here? I was a little confused at first but it started to click after I looked at how the original vector operated. Essentially, this problem is a perfect example of the recycling process. The first value gets a +1, so \(1 + 1 = 2\). Easy stuff. The second value, which would be 4, gets e added to it. Since e has an approximate value of 2.718 we get 6.718. The same happens for the third value but with \(\pi\) instead of e. Then, the fourth value cycles back to getting +1 added to it. The cycle continues all the way till the end!


Pr. 1c

The sum of every third element of the sequence {1,2,3,…100} is

sum(seq(1,100,3))
[1] 1717

Pr. 1d

Replace every third element in the sequence 1:99 with its negative starting with the 1st element.

seq(from = 1, to = 99, by = 1) * c(1, 1, -1)
 [1]   1   2  -3   4   5  -6   7   8  -9  10  11 -12  13  14 -15  16  17 -18  19
[20]  20 -21  22  23 -24  25  26 -27  28  29 -30  31  32 -33  34  35 -36  37  38
[39] -39  40  41 -42  43  44 -45  46  47 -48  49  50 -51  52  53 -54  55  56 -57
[58]  58  59 -60  61  62 -63  64  65 -66  67  68 -69  70  71 -72  73  74 -75  76
[77]  77 -78  79  80 -81  82  83 -84  85  86 -87  88  89 -90  91  92 -93  94  95
[96] -96  97  98 -99
x <- 1:99 * c(1, 1, -1)
x[1] = x[1] * -1
x
 [1]  -1   2  -3   4   5  -6   7   8  -9  10  11 -12  13  14 -15  16  17 -18  19
[20]  20 -21  22  23 -24  25  26 -27  28  29 -30  31  32 -33  34  35 -36  37  38
[39] -39  40  41 -42  43  44 -45  46  47 -48  49  50 -51  52  53 -54  55  56 -57
[58]  58  59 -60  61  62 -63  64  65 -66  67  68 -69  70  71 -72  73  74 -75  76
[77]  77 -78  79  80 -81  82  83 -84  85  86 -87  88  89 -90  91  92 -93  94  95
[96] -96  97  98 -99
1:99 * c(-1, 1, 1)
 [1]  -1   2   3  -4   5   6  -7   8   9 -10  11  12 -13  14  15 -16  17  18 -19
[20]  20  21 -22  23  24 -25  26  27 -28  29  30 -31  32  33 -34  35  36 -37  38
[39]  39 -40  41  42 -43  44  45 -46  47  48 -49  50  51 -52  53  54 -55  56  57
[58] -58  59  60 -61  62  63 -64  65  66 -67  68  69 -70  71  72 -73  74  75 -76
[77]  77  78 -79  80  81 -82  83  84 -85  86  87 -88  89  90 -91  92  93 -94  95
[96]  96 -97  98  99

The wording here confused me just a bit so I decided to do three different solutions to the problem. Personally, I think the third of my answers is the closest to what you wanted. I used both a standard vector and the more detailed sequence to test if they were equivalent which they seem to be for this situation. How exciting! Of course this is only the case because we were incrementing by 1. There is likely a better way to replace the third element with a negative but I think multiplying by -1 is a tried and true classic. It’s kind of cool to see how efficient the recycling method is.


Pr. 1e

Replace every third element in the sequence 1:99 with its negative starting with the 3rd element.

seq(from = 1, to = 99, by = 1) * c(1, 1, -1)
 [1]   1   2  -3   4   5  -6   7   8  -9  10  11 -12  13  14 -15  16  17 -18  19
[20]  20 -21  22  23 -24  25  26 -27  28  29 -30  31  32 -33  34  35 -36  37  38
[39] -39  40  41 -42  43  44 -45  46  47 -48  49  50 -51  52  53 -54  55  56 -57
[58]  58  59 -60  61  62 -63  64  65 -66  67  68 -69  70  71 -72  73  74 -75  76
[77]  77 -78  79  80 -81  82  83 -84  85  86 -87  88  89 -90  91  92 -93  94  95
[96] -96  97  98 -99
1:99 * c(1, 1, -1)
 [1]   1   2  -3   4   5  -6   7   8  -9  10  11 -12  13  14 -15  16  17 -18  19
[20]  20 -21  22  23 -24  25  26 -27  28  29 -30  31  32 -33  34  35 -36  37  38
[39] -39  40  41 -42  43  44 -45  46  47 -48  49  50 -51  52  53 -54  55  56 -57
[58]  58  59 -60  61  62 -63  64  65 -66  67  68 -69  70  71 -72  73  74 -75  76
[77]  77 -78  79  80 -81  82  83 -84  85  86 -87  88  89 -90  91  92 -93  94  95
[96] -96  97  98 -99

This one is incredibly straightforward. Same method as last time I just have the -1 in the third spot of my smaller vector.


Pr. 1f

Construct a vector of integers from 1 to 100 that are not divisible by 2, 3 or 7.

x <- 1:100

a <- which((x %% 2 != 0) & (x %% 3 != 0) & (x %% 7 != 0))
a
 [1]  1  5 11 13 17 19 23 25 29 31 37 41 43 47 53 55 59 61 65 67 71 73 79 83 85
[26] 89 95 97

Okay, this one was surprisingly insane to figure out. The solution I found I think is quite clean and readable, but it is a rearranging of an answer that I swear is equivalent yet for some reason totally breaks down. Let me include it in a code block below. Please give me feedback on why that version does not work. I am so curious because the logic seems totally sound to me. I spent an embarrassing amount of time bashing my face against this one.

# Failed code
list <- 1:100

d <- which(list %% 2 != 0)
e <- which(d %% 3 != 0)
f <- which(e %% 6 != 0)

f
 [1]  1  2  3  5  6  7  9 10 11 13 14 15 17 18 19 21 22 23 25 26 27 29 30 31 33

What I found here is that the individual lines seem to work fine. If, for example, you replace “d” or “e” after the open paran in lines 107 and 108 with “list” it works great. There seems to be some sort of breakdown when I attempt to build the logic on itself like I did. Why does this method break down? Like I said, if there’s any kind of insight you could provide I would love to hear it.

Note: I actually figured it out. I just lack basic reading comprehension skills. the which() function gives you “the indices where a logical statement is TRUE”. Straight from the lab notes. Reading is hard. That completely explains why the function worked like I thought at times and broke down the further we extended the logic. I was using the wrong tool for the job. I’m keeping my incorrect thoughts in the homework anyway.


Pr. 1g

Create a vector of 57 values equally spaced between 2.1 and 3.75

x <- seq(from = 2.1, to = 3.75, by = 0.029)
x
 [1] 2.100 2.129 2.158 2.187 2.216 2.245 2.274 2.303 2.332 2.361 2.390 2.419
[13] 2.448 2.477 2.506 2.535 2.564 2.593 2.622 2.651 2.680 2.709 2.738 2.767
[25] 2.796 2.825 2.854 2.883 2.912 2.941 2.970 2.999 3.028 3.057 3.086 3.115
[37] 3.144 3.173 3.202 3.231 3.260 3.289 3.318 3.347 3.376 3.405 3.434 3.463
[49] 3.492 3.521 3.550 3.579 3.608 3.637 3.666 3.695 3.724
length(x)
[1] 57

For this problem it took a little messing around with the by value. I’m not entirely sure if there is a more efficient way to figure out the right interval.


Pr. 1h

Give R code that will create the vector(1,2,3,4,5,6,7,8,7,6,5,4,3,2,1)

c(1:8, seq(7, 1, -1))
 [1] 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1

Pr. 1i

Give R code that will create the vector (1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5).

rep(1:5, times = 1:5)
 [1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

So this works out pretty conveniently. Essentially what’s happening under the hood is that we have a list of 1,2,3,4,5 and it repeats an amount of 1 to 5. How that is determined is based on placement in the list, so 5 is in the fifth spot so it gets repeated 5 times.


Pr. 1j

Give R code that will create the vector (1, 2, 3, 4, 5, 2, 3, 4, 5, 6, 3, 4, 5, 6, 7, 4, 5, 6, 7, 8, 5, 6, 7, 8, 9).

Before we start let’s figure out what’s happening. So, this list seems to be in batches of five numbers and it shifts one over with every repetition. So, the first loop has 1:5, the second goes 2:6, the third is 3:7 and so on. This could be done using the rep() function, and a lamer though valid solution can be done utilizing the same methodology as problem 1h.

c(1:5, 2:6, 3:7, 4:8, 5:9)
 [1] 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9

This feels like cheating but it works.


Pr. 2

Convert degrees Fahrenheit to degrees Celsius rounded to two decimal places.

Fahrenheit <- c(42,77,20,104,-25,32,212,-40)
Celsius <- (5/9)*(Fahrenheit - 32)
round(Celsius,2)
[1]   5.56  25.00  -6.67  40.00 -31.67   0.00 100.00 -40.00

Pr. 3

Create a single vector that contains the following information: i) The integers from 6 to 12 (inclusive). ii) The value 5.3 repeated 3 times. iii) The number -3. iv) A sequence of 9 integers starting at 42.

# Pr. 3a
x <- c(6:12, rep(5.3, times = 3), -3, 42:50)
x
 [1]  6.0  7.0  8.0  9.0 10.0 11.0 12.0  5.3  5.3  5.3 -3.0 42.0 43.0 44.0 45.0
[16] 46.0 47.0 48.0 49.0 50.0
# Pr. 3b Verify length is 20
length(x)
[1] 20
#Sort in ascending order
y <- sort(x)
y
 [1] -3.0  5.3  5.3  5.3  6.0  7.0  8.0  9.0 10.0 11.0 12.0 42.0 43.0 44.0 45.0
[16] 46.0 47.0 48.0 49.0 50.0
# Sort y in reverse order
z <- rev(y)
z
 [1] 50.0 49.0 48.0 47.0 46.0 45.0 44.0 43.0 42.0 12.0 11.0 10.0  9.0  8.0  7.0
[16]  6.0  5.3  5.3  5.3 -3.0
# Remove 1st, 5th and 7th terms of y
answer <- y[-c(1, 5, 7)]
answer
 [1]  5.3  5.3  5.3  7.0  9.0 10.0 11.0 12.0 42.0 43.0 44.0 45.0 46.0 47.0 48.0
[16] 49.0 50.0

Pr. 4

# Pr. A: Construct a sequence of the 1st 24 triangular numbers
n <- 1:24

triangular_numbers <- n * (n+1) / 2
triangular_numbers
 [1]   1   3   6  10  15  21  28  36  45  55  66  78  91 105 120 136 153 171 190
[20] 210 231 253 276 300
# Pr. B: Compute the sum of the 1st 24 triangular numbers
sum(triangular_numbers)
[1] 2600
# Pr. C: This is the formula for stacking dots in a triangular arrangement. All 3 sides are of equal length and as such have equal dots. This of course grows exponentially as the triangle gets larger.

# Pr. D: Compute the sum of the 1st 10 instead.
sum(triangular_numbers[1:10])
[1] 220
# Pr. E: Append zeroes until our list "a" is 26 elements long. Now, take note it currently has length 24 so we need to add two. 
adding_zeroes <- rep(0, times = 2)
w <- append(triangular_numbers, adding_zeroes, after = 24)
w
 [1]   1   3   6  10  15  21  28  36  45  55  66  78  91 105 120 136 153 171 190
[20] 210 231 253 276 300   0   0
length(w)
[1] 26
# Pr. F: Name the elements in the vector from part c using the letters of the alphabet. 
titles <- c(letters)
names(w) <- titles
w
  a   b   c   d   e   f   g   h   i   j   k   l   m   n   o   p   q   r   s   t 
  1   3   6  10  15  21  28  36  45  55  66  78  91 105 120 136 153 171 190 210 
  u   v   w   x   y   z 
231 253 276 300   0   0 
# Pr. G: Form a new vector of triangular numbers that are named with the 5 vowels. 
one_to_five <- 1:5
five_triangular_numbers <- one_to_five * (one_to_five+1) / 2

vowels <- c("a", "e", "i", "o", "u")
names(five_triangular_numbers) <- vowels
five_triangular_numbers
 a  e  i  o  u 
 1  3  6 10 15 

Pr. 5

# Pr. A: 
set.seed(50)
xvec <- sample(0:999, 250, replace = TRUE)

xvec
  [1] 367  10 819  94 862 881 813 374 706 903 271  17 986 764 276 474 339 761
 [19] 830 439 420 481 966 795 624 988 473 777 568 957 159  12 609 216 161 814
 [37] 926 905 362 250 518 852 593 835  86 325 678 455 773 996 935 180 975 259
 [55] 793 656 749  59  49 997 640 487 271 465 398 958 755 435 499 722 335 778
 [73] 915 500  94 215 805 239 810 104 755  55 868 233 125 581 441 332 257  68
 [91]  43 662 114 853 440 669 979 811 385 765 470 526 591 234 665 345 799 610
[109] 221 646 391 253 261 640 865 563 477 130 206 388 504 988 469 855 752 887
[127] 450  40 161 705 319 346 611 778 769 274 541 667 286 692 160  78 605 310
[145]  23 949  87 895 362 344 598 561 510 777 251 485 786 369 576 607 778 919
[163] 651 765 498 963 961 212 777  31 635 201 324 387 746 770 258 885 648 650
[181] 725 440 232 272 924 799 438  25 376 218 665 996 247 708 960  78 762 102
[199] 598 151 227 873 618 390 892 113 839 978 962 926 757 397 521 358 274  98
[217] 711 189 601 921 450 409 814 827  79 150 566 120 983   3 703  46 317 257
[235] 523 180 840 402 680 512 954 927 687 261 449 398 800 211 139 795
# Pr. B: 
big_number <- subset(xvec, xvec > 600)
big_number  
  [1] 819 862 881 813 706 903 986 764 761 830 966 795 624 988 777 957 609 814
 [19] 926 905 852 835 678 773 996 935 975 793 656 749 997 640 958 755 722 778
 [37] 915 805 810 755 868 662 853 669 979 811 765 665 799 610 646 640 865 988
 [55] 855 752 887 705 611 778 769 667 692 605 949 895 777 786 607 778 919 651
 [73] 765 963 961 777 635 746 770 885 648 650 725 924 799 665 996 708 960 762
 [91] 873 618 892 839 978 962 926 757 711 601 921 814 827 983 703 840 680 954
[109] 927 687 800 795
length(big_number)
[1] 112
# Pr. C
indices <- which(xvec > 600)
indices
  [1]   3   5   6   7   9  10  13  14  18  19  23  24  25  26  28  30  33  36
 [19]  37  38  42  44  47  49  50  51  53  55  56  57  60  61  66  67  70  72
 [37]  73  77  79  81  83  92  94  96  97  98 100 105 107 108 110 114 115 122
 [55] 124 125 126 130 133 134 135 138 140 143 146 148 154 157 160 161 162 163
 [73] 164 166 167 169 171 175 176 178 179 180 181 185 186 191 192 194 195 197
 [91] 202 203 205 207 208 209 210 211 217 219 220 223 224 229 231 237 239 241
[109] 242 243 247 250
# Pr. D
 sort(xvec)
  [1]   3  10  12  17  23  25  31  40  43  46  49  55  59  68  78  78  79  86
 [19]  87  94  94  98 102 104 113 114 120 125 130 139 150 151 159 160 161 161
 [37] 180 180 189 201 206 211 212 215 216 218 221 227 232 233 234 239 247 250
 [55] 251 253 257 257 258 259 261 261 271 271 272 274 274 276 286 310 317 319
 [73] 324 325 332 335 339 344 345 346 358 362 362 367 369 374 376 385 387 388
 [91] 390 391 397 398 398 402 409 420 435 438 439 440 440 441 449 450 450 455
[109] 465 469 470 473 474 477 481 485 487 498 499 500 504 510 512 518 521 523
[127] 526 541 561 563 566 568 576 581 591 593 598 598 601 605 607 609 610 611
[145] 618 624 635 640 640 646 648 650 651 656 662 665 665 667 669 678 680 687
[163] 692 703 705 706 708 711 722 725 746 749 752 755 755 757 761 762 764 765
[181] 765 769 770 773 777 777 777 778 778 778 786 793 795 795 799 799 800 805
[199] 810 811 813 814 814 819 827 830 835 839 840 852 853 855 862 865 868 873
[217] 881 885 887 892 895 903 905 915 919 921 924 926 926 927 935 949 954 957
[235] 958 960 961 962 963 966 975 978 979 983 986 988 988 996 996 997
# Pr. E
remove_third <- xvec[-seq(from = 1, to = 112, by = 3)]
remove_third
  [1]  10 819 862 881 374 706 271  17 764 276 339 761 439 420 966 795 988 473
 [19] 568 957  12 609 161 814 905 362 518 852 835  86 678 455 996 935 975 259
 [37] 656 749  49 997 487 271 398 958 435 499 335 778 500  94 805 239 104 755
 [55] 868 233 581 441 257  68 662 114 440 669 811 385 470 526 234 665 799 610
 [73] 646 391 261 640 865 563 477 130 206 388 504 988 469 855 752 887 450  40
 [91] 161 705 319 346 611 778 769 274 541 667 286 692 160  78 605 310  23 949
[109]  87 895 362 344 598 561 510 777 251 485 786 369 576 607 778 919 651 765
[127] 498 963 961 212 777  31 635 201 324 387 746 770 258 885 648 650 725 440
[145] 232 272 924 799 438  25 376 218 665 996 247 708 960  78 762 102 598 151
[163] 227 873 618 390 892 113 839 978 962 926 757 397 521 358 274  98 711 189
[181] 601 921 450 409 814 827  79 150 566 120 983   3 703  46 317 257 523 180
[199] 840 402 680 512 954 927 687 261 449 398 800 211 139 795
Built with Hugo
Theme Stack designed by Jimmy