Generating Sequences Part 1

This lesson demonstrates how to generate sequences using R code and then provides activities for further practise.

Download .Rmd here

Download word document here

Download pdf here

KS3: Generating Sequences Part 1

Lesson objectives

Generating sequences using R code

Packages used in this lesson:

Success criteria

Keywords

Remember

Knit your R markdown document frequently. Knit for the final time when you have completed the lesson. This final output file will be the one your teacher marks.

Write your code in the code chunks.

You can run your R code chunk in the Markdown file by clicking on the little arrow on the right of the chunk.

Worked Example 1

First we will tell R to generate a sequence.

This is how you generate a sequence of numbers from 1 to 100 where the terms increase by 1.

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

Use the above example R code in chunk1 to generate the following sequences. Remember to edit the numbers in the code.

Knit your document after each question to check your sequence.

Activity 1 Questions

Q1 Generate a sequence from 1 to 100 where the terms increase by 10

Q2 Generate a sequence from 5 to 95 where the terms increase by 5.

Q3 Generate a sequence from 3 to 399 where the terms increase by 4.

Q4 Generate a sequence from 1 to 9 where the terms increase by 3.

Worked Example 2

Now we will generate a sequence that starts from a number and goes up to the nth term, where the nth term is for example 10.

For example a sequence from 1 where the terms go up by 1 till the 10th term.

seq(from=1, length.out=10, by=1)
 [1]  1  2  3  4  5  6  7  8  9 10

Use the above example R code in chunk6 to generate the following sequences.

Knit your document after each question to check your sequence.

Activity 2 Questions

Q1 Generate a sequence from 1 where the terms go up by 4 up to the 100th term

Q2 Generate a sequence from 5 where the terms go up by 10 up to the 89th term

Q3 Generate a sequence from 3 where the terms go up by 3 up to the 33rd term

Worked Example 3

So the terms have increased by a particular number, but we can also decrease by a number and produce a backward sequence.

For example, we can sequence from 100 to 1 by -1

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

Use the above example R code in chunk10 to answer the following questions.

Knit your document after each question to check your sequence is correct.

Activity 3 Questions

Q1 Generate a sequence that goes down from 50 to 0 where the terms go down by 5.

Q2 Generate a sequence from 10 to -10 where the terms go down by 1.

Q3 Generate a sequence that goes down from 20 to 10 where the terms go down by 2.

KNIT YOUR DOCUMENT for the final time. This will be the version your teacher marks.

THE END

License and Citation: You can use, modify, and adapt any of the lessons, but please include the following attribution: RGirls Community. (2022, April 10). RGirls Lessons. Zenodo. https://doi.org/10.5281/zenodo.6436861