Moar R

Allocating rabbits to different feed treatments

You want to test the effect of two different feeds, A and B on the fur quality of rabbits.

You have access to 120 laboratory rabbits: 60 white rabbits and 60 brown rabbits. In the lab, all white rabbits have odd numbers between 1 and 120, while all brown rabbits have even numbers between 1 and 120.

Your first task will be to assign a feed treatment to each rabbit. Each rabbit is an individual and therefore will create variation in the data due to differences between individual rabbits. You will have assign rabbits to treatments (the feeds) randomly, to counteract this.

Note

In this context, randomly means that you do not allocate the first 30 white rabbits to the first feed treatment and the next 30 white rabbits for the second feed treatment. You also do not merely assign the first feed treatment to every second rabbit in a numeric order, and the second feed treatment to all the other rabbits in the sample. No, you do this completely random without bias. You will learn more about this concept later in your education when you are taught experimental design.

As the fur colour may also explain some variation in the data created later during the experiment, you will also need to make sure than an equal number of each colour rabbits are assigned each treatment; meaning the first feed treatment is assigned to exactly 30 white rabbits and 30 brown rabbits, and the same is the case for the second feed treatment.

NoteQuestion

In your groups, discuss what steps would you need to take to complete the achieve the random assignment of rabbits to test the two feeds?

At this stage, think about the general things you will need to do, not the specific R code you will use to complete the steps.

You will need the following steps:

  • Create a vector with all numbers of the white rabbits
  • Take a random sample of 30 rabbits from the vector of white rabbits, and assign this random sample to an object with a name indicating that this is the white rabbits on the first feed treatment
  • Create a new vector with the rabbits on the second feed treatment by excluding the random sample from the original vector of white rabbits

Repeat the above for the brown rabbits.

NoteExercise

Do the above steps that we just decided on together. The last step will be more obvious to you after you read Chapter 12 (in r4ds) on logical vectors.

CautionTip

Think about using the functions you have learned and used today including e.g., seq() and sample(), the concept of indexing/subsetting a vector and the boolean operators.

Remember: As we are working with random samples, we need to set a seed to make sure our samples are reproducible.

# Create a vector with all numbers of the white rabbits
white <- seq(from = 1, to = 120, by = 1)
white <- white[white %% 2 == 1]
white
 [1]   1   3   5   7   9  11  13  15  17  19  21  23  25  27  29  31  33  35  37
[20]  39  41  43  45  47  49  51  53  55  57  59  61  63  65  67  69  71  73  75
[39]  77  79  81  83  85  87  89  91  93  95  97  99 101 103 105 107 109 111 113
[58] 115 117 119

Notice the numbers in square brackets to the left. They make it easy for you to count that you have exactly 60 white rabbits.

# Take a random sample of 30 rabbits from the vector of white rabbits
set.seed(1234)
white_a <- sample(white, 30)
white_a
 [1]  55  31 115  43  73  87  93  17   9  75 117   7  67  77 113  51  11  29  27
[20]  83  97  79  41  39   5  61  57 103   3  59
# You can also sort this vector, as this makes it easier to check for correctness
white_a <- sort(white_a)
white_a
 [1]   3   5   7   9  11  17  27  29  31  39  41  43  51  55  57  59  61  67  73
[20]  75  77  79  83  87  93  97 103 113 115 117

Notice that we have exactly 30 rabbits, and the numbers are between 1 and 120, but otherwise no specific pattern can be seen — it is indeed random.

# Create a new vector with the rabbits on the second feed treatment
# By excluding the random sample from the original vector of white rabbits
white_b <- white[!white %in% white_a]
white_b
 [1]   1  13  15  19  21  23  25  33  35  37  45  47  49  53  63  65  69  71  81
[20]  85  89  91  95  99 101 105 107 109 111 119

Notice that the rabbits in white_b are not included in white_a, which is exactly what we want.

We can now repeat the same steps for the brown rabbits. Remember, the brown rabbits have even numbers

brown <- seq(from = 1, to = 120, by = 1)
brown <- brown[brown %% 2 == 0]
brown
 [1]   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34  36  38
[20]  40  42  44  46  48  50  52  54  56  58  60  62  64  66  68  70  72  74  76
[39]  78  80  82  84  86  88  90  92  94  96  98 100 102 104 106 108 110 112 114
[58] 116 118 120
set.seed(1234)
brown_a <- sample(brown, 30)
brown_a
 [1]  56  32 116  44  74  88  94  18  10  76 118   8  68  78 114  52  12  30  28
[20]  84  98  80  42  40   6  62  58 104   4  60
brown_a <- sort(brown_a)
brown_a
 [1]   4   6   8  10  12  18  28  30  32  40  42  44  52  56  58  60  62  68  74
[20]  76  78  80  84  88  94  98 104 114 116 118
brown_b <- brown[!brown %in% brown_a]
brown_b
 [1]   2  14  16  20  22  24  26  34  36  38  46  48  50  54  64  66  70  72  82
[20]  86  90  92  96 100 102 106 108 110 112 120

Congratulations

You now know which rabbits of each colour that will be assigned feed A and B.