Reputation: 417
Here is a piece of my dataset:
Plot Rate Rep Plant Tuber Weight
1 101 1 1 1 1 179.4
2 101 1 1 1 2 99.4
3 101 1 1 1 3 72.4
4 101 1 1 1 4 111.5
5 101 1 1 1 5 44.9
6 101 1 1 1 6 55.3
7 101 1 1 1 7 12.6
8 101 1 1 1 8 106.7
9 101 1 1 1 9 96.7
10 101 1 1 1 10 52.5
11 101 1 1 2 1 151.1
12 101 1 1 2 2 171.7
13 101 1 1 2 3 93.0
14 101 1 1 2 4 82.4
15 101 1 1 2 5 143.9
16 101 1 1 2 6 115.6
17 101 1 1 2 7 141.3
18 101 1 1 2 8 72.6
19 101 1 1 2 9 97.2
20 101 1 1 2 10 146.8
21 101 1 1 2 11 104.0
22 101 1 1 2 12 121.6
23 101 1 1 3 1 150.9
24 101 1 1 3 2 47.1
25 101 1 1 3 3 59.6
26 101 1 1 3 4 94.2
27 101 1 1 3 5 167.4
28 101 1 1 3 6 55.2
29 101 1 1 3 7 21.8
30 101 1 1 3 8 79.6
31 101 1 1 3 9 92.2
32 101 1 1 3 10 78.0
33 101 1 1 3 11 61.8
34 101 1 1 3 12 9.5
35 101 1 1 3 13 2.7
36 101 1 1 3 14 3.8
37 101 1 1 3 15 1.1
38 103 2 1 1 1 24.8
39 103 2 1 1 2 70.1
40 103 2 1 1 3 90.7
41 103 2 1 1 4 75.1
42 103 2 1 1 5 97.9
43 103 2 1 1 6 44.6
44 103 2 1 1 7 65.1
45 103 2 1 1 8 74.5
46 103 2 1 1 9 6.2
47 103 2 1 1 10 7.4
48 103 2 1 1 11 46.1
49 103 2 1 1 12 43.8
50 103 2 1 1 13 61.8
51 103 2 1 1 14 88.2
52 103 2 1 1 15 64.4
53 103 2 1 1 16 35.0
54 103 2 1 1 17 6.0
55 103 2 1 1 18 6.4
56 103 2 1 1 19 55.2
57 103 2 1 1 20 12.1
58 103 2 1 1 21 2.2
59 103 2 1 1 22 4.6
60 103 2 1 1 23 2.3
61 103 2 1 2 1 76.2
62 103 2 1 2 2 63.2
63 103 2 1 2 3 85.3
64 103 2 1 2 4 1.3
65 103 2 1 2 5 59.7
66 103 2 1 2 6 94.9
67 103 2 1 2 7 1.2
68 103 2 1 3 1 103.1
69 103 2 1 3 2 1.6
70 103 2 1 3 3 52.9
71 103 2 1 3 4 101.7
72 103 2 1 3 5 68.5
73 103 2 1 3 6 74.1
74 103 2 1 3 7 106.0
75 103 2 1 3 8 62.7
76 103 2 1 3 9 65.0
77 103 2 1 3 10 47.5
78 103 2 1 3 11 1.2
79 103 2 1 3 12 5.3
80 103 2 1 3 13 8.3
81 103 2 1 3 14 5.5
82 103 2 1 3 15 2.5
83 104 3 1 1 1 150.3
84 104 3 1 1 2 218.8
85 104 3 1 1 3 149.4
86 104 3 1 1 4 144.7
87 104 3 1 1 5 112.5
88 104 3 1 1 6 144.5
89 104 3 1 1 7 139.0
90 104 3 1 1 8 156.9
91 104 3 1 1 9 120.2
92 104 3 1 1 10 46.3
93 104 3 1 1 11 43.4
94 104 3 1 1 12 81.3
95 104 3 1 1 13 7.1
96 104 3 1 1 14 33.3
97 104 3 1 1 15 31.2
98 104 3 1 1 16 12.8
99 104 3 1 1 17 1.5
100 104 3 1 1 18 116.9
101 104 3 1 1 19 52.5
102 104 3 1 2 1 11.5
103 104 3 1 2 2 130.0
104 104 3 1 2 3 NA
105 104 3 1 2 4 125.9
106 104 3 1 2 5 103.6
107 104 3 1 2 6 43.0
108 104 3 1 2 7 79.0
109 104 3 1 2 8 79.4
110 104 3 1 2 9 51.1
111 104 3 1 2 10 1.9
112 104 3 1 2 11 4.5
113 104 3 1 2 12 17.2
114 104 3 1 2 13 58.2
115 104 3 1 2 14 71.6
116 104 3 1 2 15 80.4
117 104 3 1 2 16 44.1
118 104 3 1 2 17 62.4
119 104 3 1 2 18 52.9
120 104 3 1 2 19 28.0
121 104 3 1 2 20 89.4
122 104 3 1 2 21 62.7
123 104 3 1 2 22 55.5
124 104 3 1 2 23 0.8
125 104 3 1 2 24 22.5
126 104 3 1 2 25 2.5
127 104 3 1 2 26 1.6
128 104 3 1 2 27 46.6
129 104 3 1 3 1 191.9
130 104 3 1 3 2 153.2
131 104 3 1 3 3 137.0
132 104 3 1 3 4 90.8
133 104 3 1 3 5 152.8
134 104 3 1 3 6 69.2
135 104 3 1 3 7 11.6
136 104 3 1 3 8 58.7
137 104 3 1 3 9 53.2
138 104 3 1 3 10 68.4
139 104 3 1 3 11 46.0
140 104 3 1 3 12 75.6
141 104 3 1 3 13 68.9
142 104 3 1 3 14 94.8
143 104 3 1 3 15 89.7
This covers one of the four reps in my overall dataset. I am looking for a way to get the total collective tuber weight of the three collected plants, averaged between the four reps, for each rate. To be clear, I want to have the weight of all tubers for all 3 of the plants of each Rate/Rep combination added into one final value, then I want to get the average of this final value between the 4 unique Reps (repetitions) that make up each of the 4 Rates.
When calculating the first two reps of rate 1 by hand, I get the following:
Rate Rep TotalResponse
1 1 1 3197.5
2 1 2 2367.4
To be as clear as possible, the "TotalResponse" column shows the total sum of the "Weight" responses for each tuber in all three plants of that unique Rate/Rep combination.
When taking the average of those two responses, I get this:
Rate AvgResponse
1 1 2782.4
In reality, I need to do the first step for all 4 reps and not just 2 of them, and then I need the final table to have this average response for each of the 4 rates.
Rate AvgResponse
1 1 2782.4
2 1 xxxx
3 1 xxxx
4 1 xxxx
Thanks in advance for any help.
Upvotes: 1
Views: 64
Reputation: 887118
We may need to group by 'Rate', 'Rep', get the sum
of 'Weight', then do a group by 'Rate' and return the mean
of 'TotalResponse'
library(dplyr)
df1 %>%
group_by(Rate, Rep) %>%
summarise(TotalResponse = sum(Weight, na.rm = TRUE),
.groups = 'drop_last') %>%
group_by(Rate) %>%
summarise(AvgResponse = mean(TotalResponse))
Upvotes: 2