Weighted proportions and confidence intervals - r

I have tried to follow this post to calculate a weighted proportion and standard error. However, the answer provided did not have a lot of explanation so I was unsure if my calculations were correct.
I would love confirmation that what I've done is indeed correct, or alternate ways to achieve my desired outcome if incorrect?
# Test data
test <- structure(list(koala = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Gendry", class = "factor"),
koala.pres = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L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 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), .Label = c("Exposed Sandstone Scribbly Gum", "Sheltered sandstone Blue leafed stringybark forest",
"Transitional Shale Dry Ironbark Forest"), class = "factor"),
tree.sp = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 7L, 7L, 7L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 12L, 12L, 13L, 13L,
13L, 13L, 13L, 14L, 14L, 14L, 14L, 2L, 2L, 2L, 7L, 7L, 7L,
9L, 13L, 1L, 1L, 1L, 2L, 2L, 10L, 11L, 11L, 13L, 13L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L), .Label = c("A. littoralis",
"C. gummifera", "E. amplifolia", "E. beyeriana", "E. crebra",
"E. fibrosa", "E. globoidea", "E. longifolia", "E. oblonga",
"E. piperita", "E. punctata", "E. resinifera", "E. sclerophylla",
"E. sieberi"), class = "factor"), cbh = c(0.76, 0.98, 0.42,
0.34, 0.4, 0.44, 0.45, 0.47, 0.66, 0.59, 0.99, 0.43, 0.35,
0.36, 0.4, 0.46, 0.52, 0.49, 0.4, 1.56, 1.26, 0.83, 1.1,
1.22, 1.04, 1.04, 1.08, 1.7, 1.35, 1.89, 0.88, 0.63, 1.26,
0.45, 1.2, 1.33, 0.41, 1.22, 0.75, 0.32, 0.52, 0.6, 1.37,
1.51, 1.29, 0.51, 0.46, 0.44, 2.35, 1.68, 1.24, 0.58, 0.53,
0.69, 0.45, 0.5, 0.5, 0.51, 1.46, 1.23, 0.32, 1.47, 2.27,
0.41, 0.59, 0.61, 0.83, 0.56, 0.41, 0.47, 0.6, 0.35, 1.91,
0.65, 0.52, 1.41, 0.95, 0.91, 1.51, 1.08, 0.95, 0.52, 1.7,
0.76, 1.03, 0.88, 1.45, 1.81, 0.4, 0.39, 0.34, 0.35, 0.89,
0.8, 1.1, 1.77, 0.52, 1.23, 0.49, 0.46, 2.27, 0.41, 1.4,
0.58, 0.66, 0.41, 0.44, 0.87, 0.51, 0.57, 0.78, 1.18, 1.41,
1.13, 1, 1.48, 1.48, 0.4, 1.8, 0.78, 0.82, 1.23, 1.51, 3.82,
0.51, 1.59, 0.95, 1.04, 1.98, 1.3, 0.88, 0.52, 1, 1.27, NA,
1.07, 0.35, 1.33, 0.45, 0.63, 0.45, 0.32, 0.56, 0.68, 1.67,
1.3, 1.83, 0.58, 0.56, 0.44, 0.9, 0.99, 0.59, 0.63, 2.53,
1.33, 2.1, 0.91, 1.24, 1.13, 1.22, 1.64, 2.35, 1.07, 1.27,
1.4, 1.88, 0.56, 1.86, 1.3, 1.97, 0.92, 1.23, 0.34, 0.8),
dbh = c(0.2419, 0.3119, 0.1337, 0.1082, 0.1273, 0.1401, 0.1432,
0.1496, 0.2101, 0.1878, 0.3151, 0.1369, 0.1114, 0.1146, 0.1273,
0.1464, 0.1655, 0.156, 0.1273, 0.4966, 0.4011, 0.2642, 0.3501,
0.3883, 0.331, 0.331, 0.3438, 0.5411, 0.4297, 0.6016, 0.2801,
0.2005, 0.4011, 0.1432, 0.382, 0.4234, 0.1305, 0.3883, 0.2387,
0.1019, 0.1655, 0.191, 0.4361, 0.4806, 0.4106, 0.1623, 0.1464,
0.1401, 0.748, 0.5348, 0.3947, 0.1846, 0.1687, 0.2196, 0.1432,
0.1592, 0.1592, 0.1623, 0.4647, 0.3915, 0.1019, 0.4679, 0.7226,
0.1305, 0.1878, 0.1942, 0.2642, 0.1783, 0.1305, 0.1496, 0.191,
0.1114, 0.608, 0.2069, 0.1655, 0.4488, 0.3024, 0.2897, 0.4806,
0.3438, 0.3024, 0.1655, 0.5411, 0.2419, 0.3279, 0.2801, 0.4615,
0.5761, 0.1273, 0.1241, 0.1082, 0.1114, 0.2833, 0.2546, 0.3501,
0.5634, 0.1655, 0.3915, 0.156, 0.1464, 0.7226, 0.1305, 0.4456,
0.1846, 0.2101, 0.1305, 0.1401, 0.2769, 0.1623, 0.1814, 0.2483,
0.3756, 0.4488, 0.3597, 0.3183, 0.4711, 0.4711, 0.1273, 0.573,
0.2483, 0.261, 0.3915, 0.4806, 1.2159, 0.1623, 0.5061, 0.3024,
0.331, 0.6303, 0.4138, 0.2801, 0.1655, 0.3183, 0.4043, NA,
0.3406, 0.1114, 0.4234, 0.1432, 0.2005, 0.1432, 0.1019, 0.1783,
0.2165, 0.5316, 0.4138, 0.5825, 0.1846, 0.1783, 0.1401, 0.2865,
0.3151, 0.1878, 0.2005, 0.8053, 0.4234, 0.6685, 0.2897, 0.3947,
0.3597, 0.3883, 0.522, 0.748, 0.3406, 0.4043, 0.4456, 0.5984,
0.1783, 0.5921, 0.4138, 0.6271, 0.2928, 0.3915, 0.1082, 0.2546
), tree.hgt = c(11.2, 9, 9.2, 6.8, 6.2, 6, 6, 6.3, 12.2,
12, 16.5, 7.4, 6.2, 9.8, 9.7, 6, 9, 7.8, 9.2, 16.6, 16.6,
13.8, 14.5, 8.4, 14.2, 15.6, 15.8, 17.8, 14.2, 17.2, 11.6,
11, 16.2, 10.6, 16.2, 14.2, 7.2, 10.2, 12.4, 9.2, 8, 16,
16.8, 15.4, 15.2, 6.6, 6.8, 7.8, 16.3, 17, 12.4, 10.8, 11,
12, 8, 9, 11.2, 14.4, 14.4, 10, 7, 15.6, 18, 6.8, 9, 6, 9.4,
10, 8.2, 8.4, 9, 6, 18.8, 12.2, 7.2, 9.4, 19.2, 14.8, 21.4,
17.4, 17.8, 11.8, 17.8, 13, 14, 14.4, 16.7, 18, 7, 7.2, 5.5,
9.2, 9.6, 14, 16, 19.2, 11, 15.5, 7.2, 9, 19.5, 7.2, 23,
17.6, 11.8, 7.2, 7.5, 14, 11.6, 9.3, 16.8, 16.6, 15, 18.6,
22.8, 20, 19.8, 9, 18.2, 14, 19.2, 16.4, 19.8, 5.8, 11.8,
17.6, 17.8, 14.6, 17.6, 16.9, 16.3, 10.8, 17.8, 17, 20, 15,
8.4, 20.6, 9.2, 14, 8.5, 8.2, 11.2, 6.6, 18.4, 18.4, 21,
9.8, 9.2, 9, 15.2, 17.2, 10.4, 8.8, 19.2, 19, 25, 14.9, 19,
17.8, 11.3, 20, 23, 12, 17.9, 17.9, 15.2, 8, 17, 13, 14,
18, 19.4, 5.4, 16), rel.abu.tree.in.hr = c(18.7, 18.7, 18.7,
18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7,
18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 17.6, 17.6, 17.6, 4.78,
4.78, 4.78, 4.78, 4.78, 4.78, 4.78, 4.78, 4.78, 4.78, 4.78,
4.78, 0.74, 0.74, 2.7, 2.7, 2.7, 2.7, 2.7, 1.47, 1.47, 1.47,
1.47, 18.7, 18.7, 18.7, 17.6, 17.6, 17.6, 4.78, 2.7, 0.78,
0.78, 0.78, 18.7, 18.7, 0.26, 3.4, 3.4, 2.7, 2.7, 0.78, 0.78,
18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7,
18.7, 0.004, 9.19, 9.19, 9.19, 9.19, 9.19, 9.19, 9.19, 9.19,
9.19, 9.19, 9.19, 9.19, 9.19, 9.19, 9.19, 9.19, 9.19, 9.19,
9.19, 9.19, 9.19, 14.7, 14.7, 14.7, 14.7, 14.7, 14.7, 14.7,
14.7, 14.7, 14.7, 14.7, 14.7, 14.7, 14.7, 14.7, 17.6, 17.6,
17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6,
17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6,
17.6, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53,
16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53,
16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 3.4,
3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 0.74, 0.74,
0.74, 0.74), prop.hab.class.in.hr = c(18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 2.631578947, 2.631578947, 2.631578947, 2.631578947,
2.631578947, 2.631578947, 2.631578947, 2.631578947, 2.631578947,
2.631578947, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842), k.pres = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("0", "1"), class = "factor"),
rel_abun = c(344.473684181, 344.473684181, 344.473684181,
344.473684181, 344.473684181, 344.473684181, 344.473684181,
344.473684181, 344.473684181, 344.473684181, 344.473684181,
344.473684181, 344.473684181, 344.473684181, 344.473684181,
344.473684181, 344.473684181, 344.473684181, 344.473684181,
324.210526288, 324.210526288, 324.210526288, 88.0526315714,
88.0526315714, 88.0526315714, 88.0526315714, 88.0526315714,
88.0526315714, 88.0526315714, 88.0526315714, 88.0526315714,
88.0526315714, 88.0526315714, 88.0526315714, 13.6315789462,
13.6315789462, 49.736842101, 49.736842101, 49.736842101,
49.736842101, 49.736842101, 27.0789473661, 27.0789473661,
27.0789473661, 27.0789473661, 344.473684181, 344.473684181,
344.473684181, 324.210526288, 324.210526288, 324.210526288,
88.0526315714, 49.736842101, 2.05263157866, 2.05263157866,
2.05263157866, 49.2105263089, 49.2105263089, 0.68421052622,
8.9473684198, 8.9473684198, 7.1052631569, 7.1052631569, 61.5789473676,
61.5789473676, 1476.315789454, 1476.315789454, 1476.315789454,
1476.315789454, 1476.315789454, 1476.315789454, 1476.315789454,
1476.315789454, 1476.315789454, 1476.315789454, 1476.315789454,
0.31578947368, 725.5263157798, 725.5263157798, 725.5263157798,
725.5263157798, 725.5263157798, 725.5263157798, 725.5263157798,
725.5263157798, 725.5263157798, 725.5263157798, 725.5263157798,
725.5263157798, 725.5263157798, 725.5263157798, 725.5263157798,
725.5263157798, 725.5263157798, 725.5263157798, 725.5263157798,
725.5263157798, 725.5263157798, 1160.526315774, 1160.526315774,
1160.526315774, 1160.526315774, 1160.526315774, 1160.526315774,
1160.526315774, 1160.526315774, 1160.526315774, 1160.526315774,
1160.526315774, 1160.526315774, 1160.526315774, 1160.526315774,
1160.526315774, 1389.473684192, 1389.473684192, 1389.473684192,
1389.473684192, 1389.473684192, 1389.473684192, 1389.473684192,
1389.473684192, 1389.473684192, 1389.473684192, 1389.473684192,
1389.473684192, 1389.473684192, 1389.473684192, 1389.473684192,
1389.473684192, 1389.473684192, 1389.473684192, 1389.473684192,
1389.473684192, 1389.473684192, 1389.473684192, 1389.473684192,
1304.9999999826, 1304.9999999826, 1304.9999999826, 1304.9999999826,
1304.9999999826, 1304.9999999826, 1304.9999999826, 1304.9999999826,
1304.9999999826, 1304.9999999826, 1304.9999999826, 1304.9999999826,
1304.9999999826, 1304.9999999826, 1304.9999999826, 1304.9999999826,
1304.9999999826, 1304.9999999826, 1304.9999999826, 1304.9999999826,
1304.9999999826, 1304.9999999826, 1304.9999999826, 1304.9999999826,
1304.9999999826, 268.421052628, 268.421052628, 268.421052628,
268.421052628, 268.421052628, 268.421052628, 268.421052628,
268.421052628, 268.421052628, 268.421052628, 58.4210526308,
58.4210526308, 58.4210526308, 58.4210526308)), row.names = c(NA,
-175L), class = "data.frame")
# Calculate a weighted proportion for test$tree.sp
# Weighting variable is test$rel.abu.tree.in.hr
# Calculate weighted proportion
library(survey)
dsurvey <- svydesign(ids = ~1, data = test, weights = ~rel.abu.tree.in.hr)
wpct <- data.frame(svymean(~tree.sp, design = dsurvey))
Outcome of above
wpct
mean SE
tree.spA. littoralis 1.830415e-03 8.345005e-04
tree.spC. gummifera 3.071812e-01 4.180415e-02
tree.spE. amplifolia 1.877349e-06 1.889682e-06
tree.spE. beyeriana 4.744530e-02 1.424895e-02
tree.spE. crebra 4.313209e-02 1.359431e-02
tree.spE. fibrosa 1.034889e-01 2.547820e-02
tree.spE. globoidea 2.395497e-01 3.825613e-02
tree.spE. longifolia 1.939536e-01 3.472975e-02
tree.spE. oblonga 2.916462e-02 8.264006e-03
tree.spE. piperita 1.220277e-04 1.228147e-04
tree.spE. punctata 1.914896e-02 5.681831e-03
tree.spE. resinifera 2.083857e-03 8.701564e-04
tree.spE. sclerophylla 1.013768e-02 3.663735e-03
tree.spE. sieberi 2.759703e-03 1.400363e-03

Related

How to show the id of outliers on a boxplot

How can you view the id of the outliers in a boxplot?
structure(list(pot = c(1L, 2L, 3L, 4L, 21L, 22L, 23L, 24L, 5L,
6L, 7L, 8L, 25L, 26L, 27L, 28L, 9L, 10L, 11L, 12L, 29L, 30L,
31L, 32L, 13L, 14L, 15L, 16L, 33L, 34L, 35L, 36L, 17L, 18L, 19L,
20L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 61L, 62L, 63L, 64L,
45L, 46L, 47L, 48L, 65L, 66L, 67L, 68L, 49L, 50L, 51L, 52L, 69L,
70L, 71L, 72L, 53L, 54L, 55L, 56L, 73L, 74L, 75L, 76L, 57L, 58L,
59L, 60L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 101L, 102L,
103L, 104L, 85L, 86L, 87L, 88L, 105L, 106L, 107L, 108L, 89L,
90L, 91L, 92L, 109L, 110L, 111L, 112L, 93L, 94L, 95L, 96L, 113L,
114L, 115L, 116L, 97L, 98L, 99L, 100L, 117L, 118L, 119L, 120L,
121L, 122L, 123L, 124L, 141L, 142L, 143L,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L, 1L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("Non-waterlogged",
"Waterlogged"), class = "factor"), P = c(12.1, 12.1, 12.1, 12.1,
12.1, 12.1, 12.1, 12.1, 15.17, 15.17, 15.17, 15.17, 15.17, 15.17,
15.17, 15.17, 18.24, 18.24, 18.24, 18.24, 18.24, 18.24, 18.24,
18.24, 24.39, 24.39, 24.39, 24.39, 24.39, 24.39, 24.39, 24.39,
48.35, 48.35, 48.35, 48.35, 48.35, 48.35, 48.35, 48.35, 12.1,
12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 15.17, 15.17, 15.17,
15.17, 15.17, 15.17, 15.17, 15.17, 18.24, 18.24, 18.24, 18.24,
18.24, 18.24, 18.24, 18.24, 24.39, 24.39, 24.39, 24.39, 24.39,
24.39, 24.39, 24.39, 48.35, 48.35, 48.35, 48.35, 48.35, 48.35,
48.35, 48.35, 12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 12.1,
15.17, 15.17, 15.17, 15.17, 15.17, 15.17, 15.17, 15.17, 18.24,
18.24, 18.24, 18.24, 18.24, 18.24, 18.24, 18.24, 24.39, 24.39,
24.39, 24.39, 24.39, 24.39, 24.39, 24.39, 48.35, 48.35, 48.35,
48.35, 48.35, 48.35, 48.35, 48.35, 12.1, 12.1, 12.1, 12.1, 12.1,
12.1, 12.1, 12.1, 15.17, 15.17, 15.17, 15.17, 15.17, 15.17, 15.17,
15.17, 18.24, 18.24, 18.24, 18.24, 18.24, 18.24, 18.24, 18.24,
24.39, 24.39, 24.39, 24.39, 24.39, 24.39, 24.39, 24.39, 48.35,
48.35, 48.35, 48.35, 48.35, 48.35, 48.35, 48.35), total = c(3.66,
2.02, 1.59, 1.67, 2.12, 2.46, 1.79, 2.09, 2.03, 2.13, 1.83, 2.34,
2.66, 2.2, 1.79, 1.97, 2.17, 2.44, 1.49, 2.19, 2.92, 2.43, 1.58,
2.07, 2.48, 2.49, 1.69, 2.1, 2.38, 2.52, 2.41, 2.46, 2.22, 2.07,
1.97, 2.3, 2.48, 3.16, 1.76, 2.38, 2.81, 2.64, 2.59, 3.28, 3.18,
2.57, 2.9, 3, 2.38, 2.72, 2.58, 2.73, 3.06, 3.01, 3.01, 2.77,
2.95, 2.36, 2.91, 2.38, 3.33, 3.19, 3.17, 3.16, 3.16, 3.2, 2.58,
3.71, 3.11, 2.7, 2.92, 1.93, 2.95, 2.57, 2.68, 2.48, 3.34, 2.75,
2.52, 1.88, 1.19, 0.57, 0.64, 0.66, 1.13, 1.28, 0.85, 0.96, 1.34,
2.14, 0.63, 1.27, 1.13, 0.64, 1.21, 1.95, 1.11, 0.91, 0.75, 0.63,
1.06, 1.07, 1.05, 0.8, 1.41, 1.13, 0.75, 0.89, 1.98, 1.27, 1.01,
1, 1.16, 0.64, 0.64, 1.02, 1.03, 1.13, 0.79, 0.6, 3.88, 2.79,
2.73, 2.77, 3.54, 2.05, 1.51, 1.88, 3.86, 3.13, 1.97, 3.46, 3.98,
3.6, 2.12, 2.86, 2.95, 1.65, 1.94, 2.53, 2.21, 1.94, 2.05, 2.22,
3, 3.28, 1.55, 3.85, 2.4, 2.1, 1.98, 1.81, 2.48, 1.66, 2.06,
1.23, 3.75, 1.99, 1.67, 1.93)), class = "data.frame", row.names = c(NA,
-160L))
boxplot(total~cultivar*as.factor(P),data=x)
This is what I am after....
I have tried following example but does not work....
boxplot(total~cultivar*as.factor(P),data=x,id=list(n=Inf))
Identifying the outliers on the plot will make it easier to remove them from analysis. For some reason its not as straightforward as I thought. The post is asking me to add more details but I think there is sufficient.
You can use the car package:
library(car)
Boxplot(total ~ cultivar*as.factor(P), id.method="y", data = x)
Update:
Is it possible to flip the coordinates in car::Boxplot?
For the sake of the challenge, I tried some hacky methods. After all, I was able to rotate the plot, but it's not as conventional as it is for ggplot2::coord_flip. Here, I am just rotating the plot. So, the labels are still in their previous alignment. We can go further, remove the labels and rewrite them, but that would defeat the whole purpose of this solution which is simplicity.
library(car)
library(gridGraphics)
p <- Boxplot(total ~ cultivar*as.factor(P), id.method="y", data = x)
grab_grob <- function(){
grid.echo()
grid.grab()
}
g <- grab_grob()
grid.newpage()
pushViewport(viewport(width=0.5,angle=90))
grid.draw(g)
Unfortunately, though boxplot does return a list structure that provides the values of the outliers (e.g., boxplot(..., plot=FALSE)$out), this doesn't help here since there are equal values in other groups that are not outliers there. (In fact, I find using $out always a bit risky unless it is just one group.)
But you can use $stats to get the whisker parameters and find everything yourself. Unfortunately, this is not a one-liner.
First, though, since I don't know what you mean by "id", I'll add something to the data:
x$id <- seq_len(nrow(x))
base R
bp <- boxplot(total ~ cultivar * as.factor(P), data = x)
lims <- data.frame(nm = bp$names, t(bp$stats[c(1,5),]))
tmpx <- merge(transform(x, nm = paste(cultivar, as.factor(P), sep = ".")), lims, by = "nm", all.x = TRUE)
tmpx <- subset(tmpx, total < X1 | total > X2)
tmpx$xval <- match(tmpx$nm, bp$names)
text(total ~ xval, id, data = tmpx, adj = c(-0.5, 0.5))
Overlaying text over boxplots might be a problem for you; you can play with various shifting and/or flipping the coordinates to control this. Clipping (not shown here, but when a text label disappears out of the plot region) can also be a problem, so you might need to manually control the limits of the plot region.
dplyr
In case you like the tidyverse-way of looking at data-munging, here's an alternative that produces the same plot.
library(dplyr)
bp <- boxplot(total ~ cultivar * as.factor(P), data = x)
x %>%
mutate( nm = paste(cultivar, as.factor(P), sep = ".") ) %>%
left_join(data.frame(nm = bp$names, t(bp$stats[c(1,5),]), stringsAsFactors = FALSE),
by = "nm") %>%
filter(total < X1 | total > X2) %>%
mutate(xval = match(nm, bp$names)) %>%
text(data = ., total ~ xval, as.character(id), adj = c(-0.5, 0.5))
(Same plot.)
dplyr and ggplot2
library(dplyr)
library(ggplot2)
bp <- boxplot(total ~ cultivar * as.factor(P), data = x, plot = FALSE)
x %>%
mutate( nm = paste(cultivar, as.factor(P), sep = ".") ) %>%
left_join(data.frame(nm = bp$names, t(bp$stats[c(1,5),]), stringsAsFactors = FALSE),
by = "nm") %>%
mutate(outlier = total < X1 | total > X2) %>%
ggplot(aes(interaction(cultivar, P), total)) +
geom_boxplot() +
geom_text(aes(label = id), hjust = -0.5, data = ~ filter(., outlier)) +
coord_flip()
I chose to flip the coordinates so that the labels would be all included and shown, but it's not required for the method. One trick I used is that the data= argument to the ggplot2 functions can take an expression (I think of it as a tilde-function), which allows subsetting of the main dataset in-place. Here I use dplyr::filter, but in this case it is just as easy to use subset (base R) in case you are not otherwise using dplyr.

Trying to label only one facet in ggplot

I am trying to label only one of my facets, but i am receiving an error message when I try to apply a solution.
structure(list(pot = c(1L, 2L, 3L, 4L, 21L, 22L, 23L, 24L, 5L,
6L, 7L, 8L, 25L, 26L, 27L, 28L, 9L, 10L, 11L, 12L, 29L, 30L,
31L, 32L, 13L, 14L, 15L, 16L, 33L, 34L, 35L, 36L, 17L, 18L, 19L,
20L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 61L, 62L, 63L, 64L,
45L, 46L, 47L, 48L, 65L, 66L, 67L, 68L, 49L, 50L, 51L, 52L, 69L,
70L, 71L, 72L, 53L, 54L, 55L, 56L, 73L, 74L, 75L, 76L, 57L, 58L,
59L, 60L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 101L, 102L,
103L, 104L, 85L, 86L, 87L, 88L, 105L, 106L, 107L, 108L, 89L,
90L, 91L, 92L, 109L, 110L, 111L, 112L, 93L, 94L, 95L, 96L, 113L,
114L, 115L, 116L, 97L, 98L, 99L, 100L, 117L, 118L, 119L, 120L,
121L, 122L, 123L, 124L, 141L, 142L, 143L, 144L, 125L, 126L, 127L,
128L, 145L, 146L, 147L, 148L, 129L, 130L, 131L, 132L, 149L, 150L,
151L, 152L, 133L, 134L, 135L, 136L, 153L, 154L, 155L, 156L, 137L,
138L, 139L, 140L, 157L, 158L, 159L, 160L), rep = c(1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 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 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Dinninup",
"Riverina", "Seaton Park", "Yarloop"), class = "factor"), Waterlogging = structure(c(2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("Non-waterlogged",
"Waterlogged"), class = "factor"), P = c(12.1, 12.1, 12.1, 12.1,
12.1, 12.1, 12.1, 12.1, 15.17, 15.17, 15.17, 15.17, 15.17, 15.17,
15.17, 15.17, 18.24, 18.24, 18.24, 18.24, 18.24, 18.24, 18.24,
18.24, 24.39, 24.39, 24.39, 24.39, 24.39, 24.39, 24.39, 24.39,
48.35, 48.35, 48.35, 48.35, 48.35, 48.35, 48.35, 48.35, 12.1,
12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 15.17, 15.17, 15.17,
15.17, 15.17, 15.17, 15.17, 15.17, 18.24, 18.24, 18.24, 18.24,
18.24, 18.24, 18.24, 18.24, 24.39, 24.39, 24.39, 24.39, 24.39,
24.39, 24.39, 24.39, 48.35, 48.35, 48.35, 48.35, 48.35, 48.35,
48.35, 48.35, 12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 12.1,
15.17, 15.17, 15.17, 15.17, 15.17, 15.17, 15.17, 15.17, 18.24,
18.24, 18.24, 18.24, 18.24, 18.24, 18.24, 18.24, 24.39, 24.39,
24.39, 24.39, 24.39, 24.39, 24.39, 24.39, 48.35, 48.35, 48.35,
48.35, 48.35, 48.35, 48.35, 48.35, 12.1, 12.1, 12.1, 12.1, 12.1,
12.1, 12.1, 12.1, 15.17, 15.17, 15.17, 15.17, 15.17, 15.17, 15.17,
15.17, 18.24, 18.24, 18.24, 18.24, 18.24, 18.24, 18.24, 18.24,
24.39, 24.39, 24.39, 24.39, 24.39, 24.39, 24.39, 24.39, 48.35,
48.35, 48.35, 48.35, 48.35, 48.35, 48.35, 48.35), form = c(3.66,
2.02, 1.59, 1.67, 2.12, 2.46, 1.79, 2.09, 2.03, 2.13, 1.83, 2.34,
2.66, 2.2, 1.79, 1.97, 2.17, 2.44, 1.49, 2.19, 2.92, 2.43, 1.58,
2.07, 2.48, 2.49, 1.69, 2.1, 2.38, 2.52, 2.41, 2.46, 2.22, 2.07,
1.97, 2.3, 2.48, 3.16, 1.76, 2.38, 2.81, 2.64, 2.59, 3.28, 3.18,
2.57, 2.9, 3, 2.38, 2.72, 2.58, 2.73, 3.06, 3.01, 3.01, 2.77,
2.95, 2.36, 2.91, 2.38, 3.33, 3.19, 3.17, 3.16, 3.16, 3.2, 2.58,
3.71, 3.11, 2.7, 2.92, 1.93, 2.95, 2.57, 2.68, 2.48, 3.34, 2.75,
2.52, 1.88, 1.19, 0.57, 0.64, 0.66, 1.13, 1.28, 0.85, 0.96, 1.34,
2.14, 0.63, 1.27, 1.13, 0.64, 1.21, 1.95, 1.11, 0.91, 0.75, 0.63,
1.06, 1.07, 1.05, 0.8, 1.41, 1.13, 0.75, 0.89, 1.98, 1.27, 1.01,
1, 1.16, 0.64, 0.64, 1.02, 1.03, 1.13, 0.79, 0.6, 3.88, 2.79,
2.73, 2.77, 3.54, 2.05, 1.51, 1.88, 3.86, 3.13, 1.97, 3.46, 3.98,
3.6, 2.12, 2.86, 2.95, 1.65, 1.94, 2.53, 2.21, 1.94, 2.05, 2.22,
3, 3.28, 1.55, 3.85, 2.4, 2.1, 1.98, 1.81, 2.48, 1.66, 2.06,
1.23, 3.75, 1.99, 1.67, 1.93)), class = "data.frame", row.names = c(NA,
-160L))
library(Rmisc)
library(ggplot2)
tglf2 <- summarySE(iso, measurevar="form", groupvars=c("P","cultivar","Waterlogging"),na.rm=TRUE)
pd <- position_dodge(0.5)
p=ggplot(tglf2, aes(x=P, y=form,colour=cultivar,group=cultivar)) +
geom_errorbar(aes(ymin=form-se, ymax=form+se),colour="black",
width=.2,position=pd) +
geom_line(position=pd) +
geom_point(aes(shape=cultivar),size=3.5,position=pd)+
scale_shape_manual(values=c(0, 16, 17,1))+
scale_color_manual(values=c("#009E73", "#F0E442", "#0072B2", "#D55E00"))+
facet_grid(~Waterlogging)
ann_text <- data.frame(P = 30,shoot = 3,lab = "Text",
Waterlogging = factor(Waterlogged,levels = c("Non-waterlogged","Waterlogged")))
p + geom_text(data = ann_text,label = "Text")
WHen I try to run the ann_text line I get an error message that it cant find the object waterlogging. I am trying to find the solution found here Annotating text on individual facet in ggplot2
Here is a workaround adding some variables to the original data. I just added three columns filled with NA, except for the position where you want your text and the text itself (making sure it is located in the facet level where you want your label, i.e., waterlogged).
#Adding the three variables to the data
tglf2 <- data.frame(tglf2,
P2 = c(rep(NA,(nrow(tglf2)-1)),30),
text = c(rep(NA,(nrow(tglf2)-1)),"MyText"),
form2 = c(rep(NA,(nrow(tglf2)-1)),3))
As the text information is already contained in the data, you just need to add the following code to your plot.
geom_text(aes(x = P2, y = form2, label = text),
col = "black")
Here is the final result

Connecting points by levels of a variable

I have a dataset that I'm using in RStudio, and I have the code ggplot(desktop_2015) + geom_point(aes(Month, CV, color = Day), size = 2.5) in order to get a graph that I need.
I am plotting the variable CV by Month, and for each month there are 7 points along the vertical, each a different color representing a level of the variable Day.
What I am trying to do is connect the points for each day across the months, ie a line connecting each Friday point across the 12 months, and so on for each day of the week. I have attached images of what my dataset looks like in addition to the graph I currently have. TIA!
Here's the dput output of my dataset:
structure(list(Year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("2015",
"2016", "2017", "2018", "2019"), class = "factor"), Quarter = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L), .Label = c("1", "2", "3", "4"), class = "factor"),
Month = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L,
12L, 12L, 12L, 12L), .Label = c("Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), class = "factor"),
Device = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("D", "M", "T"), class = "factor"), Day = structure(c(4L,
2L, 6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L, 7L, 5L, 1L, 3L, 4L, 2L,
6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L,
7L, 5L, 1L, 3L, 4L, 2L, 6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L, 7L,
5L, 1L, 3L, 4L, 2L, 6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L, 7L, 5L,
1L, 3L, 4L, 2L, 6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L, 7L, 5L, 1L,
3L, 4L, 2L, 6L, 7L, 5L, 1L, 3L), .Label = c("Friday", "Monday",
"Saturday", "Sunday", "Thursday", "Tuesday", "Wednesday"), class = "factor"),
Clicks = c(1479, 1631, 1471, 1382, 1926, 1724, 1928, 1233,
1380, 1164, 1145, 1187, 1082, 1201, 1927, 1825, 1592, 1232,
1225, 1181, 1320, 1437, 1357, 1487, 1769, 1655, 1256, 1318,
1512, 1508, 1358, 1176, 1111, 1364, 1316, 1441, 2131, 1956,
1455, 1431, 1280, 1288, 2106, 2326, 2109, 2474, 2397, 2200,
1721, 2598, 2767, 2112, 2045, 1997, 1771, 2352, 2075, 2441,
2670, 2543, 1973, 1876, 1920, 2206, 2529, 2134, 2000, 2514,
2551, 2758, 3087, 3219, 2314, 2150, 1906, 1997, 2335, 1957,
2272, 2617, 2489, 2199, 1657, 1945), Conversions = c(67,
95, 110, 101, 88, 105, 114, 89, 92, 79, 67, 72, 96, 76, 139,
125, 89, 47, 63, 73, 78, 97, 127, 69, 96, 61, 50, 90, 83,
91, 85, 56, 117, 66, 94, 48, 86, 71, 63, 53, 46, 56, 67,
75, 64, 64, 63, 55, 59, 74, 71, 62, 59, 57, 40, 71, 69, 84,
80, 101, 61, 76, 56, 93, 69, 50, 47, 73, 67, 98, 108, 127,
59, 67, 68, 88, 77, 60, 69, 82, 72, 55, 44, 54), CV = c(9089.21,
7811.24, 13201.19, 11394.8, 12631.15, 12389.61, 11742.6,
10265.62, 12449.76, 9329.68, 8255.08, 9002.71, 13173.41,
6235.05, 15480.72, 17940.65, 13667.19, 5766.98, 7583.03,
6817.59, 6412.43, 10441.66, 23018.46, 9243.69, 10521.5, 15117.06,
5791.93, 7783.52, 8156.31, 9996.18, 12973.64, 6329.24, 20080.53,
6289.64, 10891.91, 7176.93, 10281.64, 10292.1, 10077.85,
9299.89, 5979.86, 6888.64, 6799.56, 13162.34, 10267.85, 10599.65,
8868.4, 7285.48, 8393, 9930.09, 10857.6, 12568.96, 9998.93,
8465.09, 6733.55, 11107.85, 10919.87, 12933.21, 14653.55,
22648.43, 13272.86, 15214.25, 9733.4, 18128.61, 12915.65,
10267.21, 9804.48, 11928.58, 14135.84, 19990.35, 15482.84,
20116.57, 12705.79, 12891.93, 11266.43, 16632.9, 11890.34,
9290.67, 11417.62, 18980.21, 11025.63, 7806.93, 7246.12,
7737.87), `Conv. rate` = c(0.0453, 0.0582, 0.0748, 0.0731,
0.0457, 0.0609, 0.0591, 0.0722, 0.0667, 0.0679, 0.0585, 0.0607,
0.0887, 0.0633, 0.0721, 0.0685, 0.0559, 0.0381, 0.0514, 0.0618,
0.0591, 0.0675, 0.0936, 0.0464, 0.0543, 0.0369, 0.0398, 0.0683,
0.0549, 0.0603, 0.0626, 0.0476, 0.1053, 0.0484, 0.0714, 0.0333,
0.0404, 0.0363, 0.0433, 0.037, 0.0359, 0.0435, 0.0318, 0.0322,
0.0303, 0.0259, 0.0263, 0.025, 0.0343, 0.0285, 0.0257, 0.0294,
0.0289, 0.0285, 0.0226, 0.0302, 0.0333, 0.0344, 0.03, 0.0397,
0.0309, 0.0405, 0.0292, 0.0422, 0.0273, 0.0234, 0.0235, 0.029,
0.0263, 0.0355, 0.035, 0.0395, 0.0255, 0.0312, 0.0357, 0.0441,
0.033, 0.0307, 0.0304, 0.0313, 0.0289, 0.025, 0.0266, 0.0278
), `CV/Click` = c(6.15, 4.79, 8.97, 8.25, 6.56, 7.19, 6.09,
8.33, 9.02, 8.02, 7.21, 7.58, 12.18, 5.19, 8.03, 9.83, 8.58,
4.68, 6.19, 5.77, 4.86, 7.27, 16.96, 6.22, 5.95, 9.13, 4.61,
5.91, 5.39, 6.63, 9.55, 5.38, 18.07, 4.61, 8.28, 4.98, 4.82,
5.26, 6.93, 6.5, 4.67, 5.35, 3.23, 5.66, 4.87, 4.28, 3.7,
3.31, 4.88, 3.82, 3.92, 5.95, 4.89, 4.24, 3.8, 4.72, 5.26,
5.3, 5.49, 8.91, 6.73, 8.11, 5.07, 8.22, 5.11, 4.81, 4.9,
4.74, 5.54, 7.25, 5.02, 6.25, 5.49, 6, 5.91, 8.33, 5.09,
4.75, 5.03, 7.25, 4.43, 3.55, 4.37, 3.98), Impressions = c(86045,
89512, 81503, 81356, 101254, 95972, 100790, 73492, 81709,
71678, 67884, 68429, 61978, 69537, 99440, 99735, 95689, 71773,
71414, 65363, 69422, 77640, 76419, 81980, 97540, 90953, 67780,
68886, 81265, 79079, 70807, 65774, 59298, 72504, 71965, 92817,
132684, 120931, 93380, 89791, 82604, 79651, 121598, 141042,
132627, 167622, 146056, 133295, 103366, 151998, 170043, 142676,
126557, 121835, 121060, 139303, 113975, 127019, 151171, 140981,
110230, 108527, 106218, 123960, 136940, 123136, 120845, 145673,
136340, 144527, 185146, 210133, 157902, 135150, 124981, 132650,
136682, 127909, 156160, 219576, 187283, 143617, 107303, 128768
), Cost = c(1376.23, 1799.57, 1646.93, 1631.22, 2088.67,
1869.83, 1779.56, 1152.91, 1643.25, 1281.38, 1368.1, 1299.16,
1184.99, 1183.82, 1690.38, 2065.43, 1737.26, 1351.85, 1432.21,
1395.46, 1192.53, 1385.88, 1548.41, 1754.96, 2148.9, 2061.52,
1481.82, 1400.12, 1595.65, 1808.54, 1643.06, 1417.31, 1343.52,
1794.69, 1317.59, 1436.56, 2344.1, 2124.41, 1602.12, 1449.17,
1417.73, 1337.39, 1773.49, 2018.75, 1813.7, 2181.56, 2069.48,
1938.4, 1528.46, 1907.15, 2163.95, 1645.47, 1620.2, 1552.78,
1326.68, 1749.51, 1466.75, 1851.91, 1997.14, 1909.85, 1506.9,
1391.86, 1420.54, 1671.03, 1948.89, 1657.35, 1577.12, 1888.6,
1934.2, 2055.61, 2357.6, 2426.16, 1730.51, 1652.82, 1464.03,
1550.73, 1736.98, 1364.01, 1625.97, 1835.38, 1714.8, 1584.55,
1109.67, 1340.77)), row.names = c(2L, 4L, 7L, 12L, 15L, 18L,
19L, 23L, 26L, 28L, 31L, 36L, 38L, 40L, 44L, 47L, 51L, 52L, 57L,
58L, 63L, 64L, 69L, 72L, 74L, 78L, 81L, 82L, 85L, 89L, 92L, 95L,
97L, 100L, 105L, 107L, 111L, 113L, 116L, 119L, 121L, 124L, 127L,
130L, 135L, 136L, 141L, 142L, 147L, 149L, 152L, 154L, 158L, 161L,
163L, 167L, 171L, 174L, 177L, 178L, 181L, 185L, 188L, 191L, 194L,
198L, 201L, 202L, 207L, 208L, 211L, 215L, 218L, 221L, 225L, 228L,
230L, 232L, 236L, 238L, 242L, 246L, 247L, 250L), class = "data.frame")
I think you need to provide both group and color in your aes:
library(ggplot2)
ggplot(df, aes(x = Month, y = CV, color = Day, group = Day))+
geom_point()+
geom_line()

Different results in Stata and R with the "same" anova code

I have some Stata code and I want to replicate the results in R. However, even with the same dataset and, I think, the same code, I get different results in R from those in Stata. I think it could be because Stata makes the order of the regression different than keyed in.
Do I need exactly the same order as in Stata to get the same results and how can I do this?
I changed all the variables to factors and tried again but the problem is still there.
I noticed that when I change the order of the explanatory variables I get different results, but I don`t find "the right order" to replicate the Stata results.
Stata code:
. anova testm2 c.testm1 i.hptreat c.cortm1 c.cortm2 i.female if inelig == 0 & anyoutv1 == 0
Number of obs =39 R-squared =0.7048
Root MSE= 16.0144 Adj R-squared =0.6601
Source | Partial SS df MS F Prob>F
---------------------------------------------------------------
Model | 20209.281 5 4041.8563 15.76 0.0000
testm1 | 3516.6527 1 3516.6527 13.71 0.0008
hptreat| 1183.5007 1 1183.5007 4.61 0.0391
cortm1 | 8.5753841 1 8.5753841 0.03 0.8560
cortm2 | 2810.9353 1 2810.9353 10.96 0.0023
female | 2557.3444 1 2557.3444 9.97 0.0034
Residual| 8463.2532 33 256.46222
----------------------------------------------------------------
Total | 28672.535 38 754.54038
R code:
FosseTest<-aov(testm2~testm1+hptreat+cortm1+cortm2+female,data=X2data)
summary(FosseTest)
Df Sum Sq Mean Sq F value Pr(>F)
testm1 1 15121 15121 58.962 7.68e-09 ***
hptreat 1 524 524 2.043 0.16228
cortm1 1 23 23 0.089 0.76715
cortm2 1 1984 1984 7.735 0.00888 **
female 1 2557 2557 9.972 0.00339 **
Residuals 33 8463 256
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
You can see that I get totally different values in the replication.
in the X2data Set I already subset the values for if inelig == 0 & anyoutv1 == 0
for the reconstruction of the data:
dput(X2data)
structure(list(id = c(29L, 30L, 31L, 32L, 34L, 35L, 36L, 37L,
39L, 41L, 42L, 43L, 44L, 46L, 47L, 49L, 50L, 51L, 52L, 53L, 54L,
55L, 57L, 58L, 59L, 60L, 61L, 62L, 64L, 65L, 66L, 67L, 68L, 69L,
70L, 71L, 72L, 73L, 74L), inelig = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("Analytic sample (keep)", "Ineligible (drop)"
), class = "factor"), ccydrop = c(0L, 0L, 0L, 0L, 1L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), cortm1v2 = c(0.003, 0.086, 0.047, 0.106, NA, 0.153, 0.086,
0.005, 0.133, 0.036, 0.03, 0.015, 0.014, 0.111, 0.389, 0.298,
0.4, 0.215, 0.062, 0.021, 0.075, 0.073, 0.033, 0.243, 0.126,
0.147, 0.019, 0.048, 0.28, 0.052, 0.039, 0.105, 0.111, 0.133,
0.065, 0.051, 0.143, 0.127, 0.095), cortm2v2 = c(0.025, 0.167,
0.059, 0.112, 0.171, 0.183, 0.102, 0.018, 0.08, 0.015, 0.027,
0.05, 0.025, 0.046, 0.085, 0.144, 0.155, 0.09, 0.057, 0.023,
0.038, 0.205, 0.035, 0.198, 0.112, 0.211, 0.042, 0.142, 0.328,
0.076, 0.067, 0.094, 0.245, 0.153, 0.115, 0.127, 0.257, 0.125,
0.096), cdiffv2 = c(0.022, 0.081, 0.012, 0.006, NA, 0.03, 0.016,
0.013, -0.053, -0.021, -0.003, 0.035, 0.011, -0.065, -0.304,
-0.154, -0.245, -0.125, -0.005, 0.002, -0.037, 0.132, 0.002,
-0.045, -0.014, 0.064, 0.023, 0.094, 0.048, 0.024, 0.028, -0.011,
0.134, 0.02, 0.05, 0.076, 0.114, -0.002, 0.001), testm1v2 = c(38.72,
32.77, 32.32, 17.99, 73.58, 80.69, 48.56, 21.92, 27.24, 40.93,
31.73, 60.05, 38.04, 30.17, 59.07, 26.92, 25.41, 47.81, 63.02,
34.49, 104.38, 38.08, 30.99, 35.23, 104.81, 49.33, 50.03, 11.65,
143.57, 48.31, 90.37, 48.56, 41.67, 75.23, 60.56, 39.03, 18.16,
37.9, 84.5), testm2v2 = c(62.37, 29.23, 27.51, 28.66, 44.67,
105.48, 42.67, 15.01, 21.33, 10.87, 2.14, 44.53, 35.8, 10.43,
47.54, 48.5, 38.98, 91.32, 52.94, 22.43, 58.68, 81.63, 34.79,
38.57, 94.86, 50.83, 55.75, 45.33, 111.62, 65.15, 81.08, 50.08,
44.86, 58.63, 85.85, 58.69, 16.35, 35.97, 99.08), tdiffv2 = c(23.65,
-3.54, -4.81, 10.67, -28.91, 24.79, -5.89, -6.91, -5.91, -30.06,
-29.59, -15.52, -2.24, -19.74, -11.53, 21.58, 13.57, 43.51, -10.08,
-12.06, -45.7, 43.55, 3.8, 3.34, -9.95, 1.5, 5.72, 33.68, -31.95,
16.84, -9.29000000000001, 1.52, 3.19, -16.6, 25.29, 19.66, -1.81,
-1.93, 14.58), testoutv1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("Not selected", "Selected"), class = "factor"),
cortoutv1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("Not selected", "Selected"), class = "factor"),
anyoutv1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("Not selected", "Selected"), class = "factor"),
testoutv2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("Not selected", "Selected"), class = "factor"),
cortoutv2 = structure(c(1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("Not selected", "Selected"), class = "factor"),
anyoutv2 = structure(c(1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("Not selected", "Selected"), class = "factor"),
pose1rate = c(6L, 7L, 6L, 6L, 7L, 7L, 6L, 7L, 5L, 6L, 7L,
4L, 7L, 7L, 7L, 6L, 7L, 7L, 7L, 7L, 6L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L), pose2rate = c(6L,
6L, 5L, 7L, 7L, 7L, 7L, 7L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 6L,
6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 6L, 7L, 6L, 7L, 7L, 7L,
6L, 7L, 7L, 7L, 7L, 7L, 6L, 6L), poseratem = c(6, 6.5, 5.5,
6.5, 7, 7, 6.5, 7, 5.5, 6.5, 7, 5.5, 7, 7, 7, 6, 6.5, 7,
7, 7, 6.5, 7, 7, 7, 7, 6.5, 7, 6.5, 7, 7, 7, 6.5, 7, 7, 7,
7, 7, 6.5, 6.5), saldiff = c(24.30555556, 20.83333333, 29.16666667,
18.75, 23.61111111, 34.02777778, 18.05555556, 19.44444444,
21.52777778, 15.97222222, 22.91666667, 13.88888889, 22.22222222,
25, 22.22222222, 22.22222222, 18.05555556, 17.36111111, 22.22222222,
27.08333333, 20.83333333, 24.30555556, 22.22222222, 28.47222222,
24.30555556, 25, 27.77777778, 22.22222222, 15.97222222, 24.30555556,
21.52777778, 19.44444444, 15.97222222, 15.27777778, 15.97222222,
24.30555556, 19.44444444, 24.30555556, 15.27777778), sal2manip = c(19.80555556,
16.33333333, 24.66666667, 14.25, 19.11111111, 29.52777778,
13.55555556, 14.94444444, 17.02777778, 11.47222222, 18.41666667,
9.38888889, 17.72222222, 20.5, 17.72222222, 17.72222222,
13.55555556, 12.86111111, 17.72222222, 22.58333333, 16.33333333,
19.80555556, 17.72222222, 23.97222222, 19.80555556, 20.5,
23.27777778, 17.72222222, 11.47222222, 19.80555556, 17.02777778,
14.94444444, 11.47222222, 10.77777778, 11.47222222, 19.80555556,
14.94444444, 19.80555556, 10.77777778), hptreat = structure(c(2L,
1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("0", "1"), class = "factor"),
female = structure(c(1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L
), .Label = c("0", "1"), class = "factor"), age = c(19L,
20L, 20L, 18L, 21L, 20L, 18L, 21L, 35L, 20L, 18L, 20L, 20L,
18L, 20L, 25L, 18L, 23L, 21L, 19L, 20L, 20L, 30L, 19L, 22L,
18L, 19L, 22L, 19L, 20L, 28L, 28L, 19L, 19L, 20L, 25L, 20L,
25L, 23L), cort1a1 = c(0.004, 0.085, 0.049, 0.107, 0.486,
0.159, 0.088, 0.004, 0.138, 0.035, 0.03, 0.018, 0.017, 0.111,
0.39, 0.292, 0.396, 0.213, 0.065, 0.022, 0.074, 0.077, 0.035,
0.241, 0.126, 0.154, 0.021, 0.05, 0.296, 0.054, 0.04, 0.109,
0.114, 0.133, 0.063, 0.055, 0.149, 0.134, 0.098), cort1a2 = c(0.001,
0.086, 0.045, 0.105, 0.482, 0.147, 0.085, 0.005, 0.127, 0.037,
0.031, 0.013, 0.011, 0.111, 0.389, 0.304, 0.405, 0.218, 0.059,
0.02, 0.076, 0.069, 0.032, 0.246, 0.126, 0.141, 0.017, 0.046,
0.264, 0.051, 0.038, 0.101, 0.109, 0.133, 0.068, 0.048, 0.137,
0.12, 0.092), cort2a1 = c(0.027, 0.174, 0.056, 0.111, 0.175,
0.179, 0.103, 0.021, 0.079, 0.014, 0.028, 0.051, 0.024, 0.051,
0.083, 0.148, 0.156, 0.086, 0.062, 0.024, 0.038, 0.209, 0.036,
0.199, 0.114, 0.207, 0.041, 0.141, 0.333, 0.078, 0.065, 0.088,
0.238, 0.157, 0.119, 0.132, 0.268, 0.132, 0.099), cort2a2 = c(0.023,
0.161, 0.062, 0.113, 0.166, 0.188, 0.101, 0.016, 0.081, 0.015,
0.026, 0.049, 0.026, 0.041, 0.086, 0.139, 0.154, 0.093, 0.052,
0.022, 0.038, 0.202, 0.034, 0.198, 0.111, 0.215, 0.042, 0.142,
0.324, 0.075, 0.068, 0.101, 0.252, 0.149, 0.111, 0.123, 0.247,
0.118, 0.093), cortm1 = c(0.0024999999, 0.085500002, 0.046999998,
0.106, 0.484, 0.153, 0.086499996, 0.0044999998, 0.13249999,
0.035999998, 0.0305, 0.0155, 0.014, 0.111, 0.38949999, 0.29800001,
0.4005, 0.2155, 0.061999999, 0.021, 0.075000003, 0.072999999,
0.033500001, 0.24349999, 0.126, 0.14749999, 0.018999999,
0.048, 0.28, 0.052499998, 0.039000001, 0.105, 0.1115, 0.133,
0.065499999, 0.0515, 0.14300001, 0.127, 0.094999999), cortm2 = c(0.025,
0.1675, 0.059, 0.112, 0.1705, 0.18350001, 0.102, 0.0185,
0.079999998, 0.0145, 0.027000001, 0.050000001, 0.025, 0.046,
0.0845, 0.1435, 0.155, 0.089500003, 0.057, 0.023, 0.037999999,
0.20550001, 0.035, 0.19850001, 0.1125, 0.211, 0.041499998,
0.1415, 0.3285, 0.076499999, 0.066500001, 0.094499998, 0.245,
0.153, 0.115, 0.1275, 0.25749999, 0.125, 0.096000001), cdiff = c(0.022500001,
0.082000002, 0.012000002, 0.0060000047, -0.31349999, 0.03050001,
0.015500002, 0.014, -0.052499995, -0.021499999, -0.0034999996,
0.034500003, 0.011, -0.064999998, -0.30500001, -0.15450001,
-0.2455, -0.12599999, -0.004999999, 0.0020000003, -0.037000004,
0.13250001, 0.0014999993, -0.044999987, -0.013500005, 0.063500002,
0.022499999, 0.093499996, 0.048500001, 0.024, 0.0275, -0.010499999,
0.13350001, 0.019999996, 0.049500003, 0.075999998, 0.11449999,
-0.0020000041, 0.001000002), test1a1 = c(39.87, 33.22, 32.52,
19.74, 78.85, 83.51, 48.37, 22.31, 28.17, 41.44, 32.92, 61.4,
40.31, 30.36, 59.44, 27.52, 26.14, 46.75, 63.73, 34.03, 98.47,
36.62, 30.26, 37.15, 105.64, 47.99, 50.15, 11.33, 149.12,
48.57, 92.04, 51.22, 42.25, 77.07, 62.75, 38.8, 17.91, 40.28,
88.47), test1a2 = c(37.58, 32.32, 32.12, 16.25, 68.31, 77.88,
48.75, 21.53, 26.32, 40.42, 30.55, 58.7, 35.78, 29.97, 58.7,
26.32, 24.69, 48.87, 62.32, 34.95, 110.29, 39.53, 31.72,
33.32, 103.99, 50.67, 49.9, 11.97, 138.02, 48.05, 88.7, 45.89,
41.08, 73.39, 58.38, 39.25, 18.41, 35.53, 80.54), test2a1 = c(64.22,
29.43, 27.98, 28.17, 46.14, 105.92, 43.68, 16.41, 21.42,
11.35, 1.66, 44.17, 38.58, 11.11, 48.57, 48.31, 39.71, 92.04,
52.73, 22.3, 58.23, 82.01, 35.76, 39.59, 94.06, 50.52, 55.82,
45.91, 115.13, 67.59, 82.97, 49.89, 45.09, 57.86, 86.76,
58.83, 16.53, 36.7, 100.4), test2a2 = c(60.53, 29.04, 27.04,
29.14, 43.2, 105.05, 41.66, 13.62, 21.25, 10.39, 2.63, 44.9,
33.02, 9.75, 46.52, 48.7, 38.25, 90.59, 53.15, 22.57, 59.14,
81.24, 33.81, 37.55, 95.66, 51.14, 55.69, 44.74, 108.1, 62.71,
79.18, 50.27, 44.63, 59.39, 84.94, 58.55, 16.16, 35.24, 97.75
), testm1 = c(38.724998, 32.77, 32.32, 17.995001, 73.580002,
80.695, 48.560001, 21.92, 27.245001, 40.93, 31.735001, 60.049999,
38.044998, 30.165001, 59.07, 26.92, 25.415001, 47.810001,
63.025002, 34.490002, 104.38, 38.075001, 30.99, 35.235001,
104.815, 49.330002, 50.025002, 11.65, 143.57001, 48.310001,
90.370003, 48.555, 41.665001, 75.230003, 60.564999, 39.025002,
18.16, 37.904999, 84.504997), testm2 = c(62.375, 29.235001,
27.51, 28.655001, 44.669998, 105.485, 42.669998, 15.015,
21.334999, 10.87, 2.145, 44.535, 35.799999, 10.43, 47.544998,
48.505001, 38.98, 91.315002, 52.939999, 22.434999, 58.685001,
81.625, 34.785, 38.57, 94.860001, 50.830002, 55.755001, 45.325001,
111.615, 65.150002, 81.074997, 50.080002, 44.860001, 58.625,
85.849998, 58.689999, 16.344999, 35.970001, 99.074997), tdiff = c(23.650002,
-3.5349998, -4.8099995, 10.66, -28.910004, 24.790001, -5.8900032,
-6.9049997, -5.9100018, -30.060001, -29.59, -15.514999, -2.2449989,
-19.735001, -11.525002, 21.585001, 13.564999, 43.505001,
-10.085003, -12.055002, -45.694996, 43.549999, 3.7950001,
3.3349991, -9.9550018, 1.5, 5.7299995, 33.675003, -31.955009,
16.84, -9.2950058, 1.5250015, 3.1949997, -16.605003, 25.285,
19.664997, -1.8150005, -1.9349976, 14.57), feelpower = structure(c(2L,
3L, 1L, 2L, 1L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 3L, 1L, 2L, 1L,
1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 4L, 3L,
4L, 3L, 1L, 3L, 4L, 2L, 2L, 3L), .Label = c("2", "3", "Not at all",
"Very much"), class = "factor"), incharge = structure(c(1L,
1L, 3L, 4L, 1L, 2L, 3L, 3L, 1L, 1L, 3L, 4L, 3L, 2L, 2L, 1L,
3L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L,
1L, 3L, 1L, 1L, 4L, 3L, 1L, 1L), .Label = c("2", "3", "Not at all",
"Very much"), class = "factor"), powm = structure(c(3L, 1L,
1L, 5L, 2L, 4L, 6L, 6L, 1L, 1L, 6L, 7L, 6L, 3L, 4L, 2L, 1L,
4L, 4L, 3L, 2L, 4L, 2L, 2L, 3L, 3L, 3L, 4L, 1L, 5L, 1L, 4L,
6L, 2L, 1L, 7L, 2L, 3L, 1L), .Label = c("1.5", "2", "2.5",
"3", "3.5", "Not at all", "Very much"), class = "factor"),
diceroll = structure(c(2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L
), .Label = c("No", "Yes"), class = "factor")), row.names = c(2L,
3L, 4L, 5L, 7L, 8L, 9L, 10L, 12L, 14L, 15L, 16L, 17L, 19L, 20L,
22L, 23L, 24L, 25L, 26L, 27L, 28L, 30L, 31L, 32L, 33L, 34L, 35L,
37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L), class = "data.frame")
You can get the same results in R using drop1(FosseTest, test = "F"). This will test the effect of leaving one of the variables off the aov.
drop1(FosseTest, test = "F")
#
# Single term deletions
#
# Model:
# testm2 ~ testm1 + hptreat + cortm1 + cortm2 + female
# Df Sum of Sq RSS AIC F value Pr(>F)
# <none> 8463.3 221.82
# testm1 1 3516.7 11979.9 233.37 13.7122 0.0007751 ***
# hptreat 1 1183.5 9646.8 224.92 4.6147 0.0391333 *
# cortm1 1 8.6 8471.8 219.86 0.0334 0.8560279
# cortm2 1 2810.9 11274.2 231.00 10.9604 0.0022605 **
# female 1 2557.3 11020.6 230.11 9.9716 0.0033895 **
# ---
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
summary(FosseTest) displays the sequential effect of addeding the variables one after another.
There was a different way how to access this, but at the moment I can't remember...

Using cast() or ddply() to summarise the mean for two continuous variables in one dataframe

The data (below) has two columns named "Date" and "Independent Variable (IV)" containing factors, plus two extra columns called "Independent_value" and "Sapflow" containing continuous values.
Column Descriptions:
Date = measurements of the independent variables over 5 months (June-October).
Independent Variable = 3 independent variables (i.e temperature, humidity, and radiation).
Independent Value = represents readings of temperature, radiation, and humidity over daily time steps from June to October.
Sapflow (dependent variable) = sapflow rates in tree species recorded over daily time steps from June to October and how the independent variables may affect these rates of sapflow.
Goal
In this instance, I would like to summarise the data (found below) by group (i.e. Date and Independent variable) using either cast() or ddply() to produce a new data frame showing the mean recorded value of each independent variable (temperature, humidity, and radiation) per month and the mean rate of sapflow for independent variables per month in the following format:-
*Key
*IV = independent variable (i.e.Temperature, Humidity, and Radation)
*Mean_IV = the mean of the independent variable
*Mean_Sapflow (dependent variable) = the mean sapflow rate per month per independent variable
Date IV Mean_IV Mean_Sapflow
1 June Humidity 19.67 14.97
2 June Humidity 18.82 16.31
3 June Humidity 20.38 17.52
4 June Humidity 14.94 7.45
5 June Humidity 12.92 12.18
6 June Humidity 15.28 15.82
Problem:
I have tried using ddply() and cast() and I cannot produce the dataframe format shown above. If anyone can help, I would be deeply appreciative.
*ddply
library(plyr)
summarised_Sapflow<-ddply(Sapflow_new, c("Date", "Independent_Variable"), summarise,
N=length(Independent_Value),
mean("Independent Value","Sapflow"))
The output is a series of warnings:
Warning messages:
1: In mean.default("Independent Value", "Sapflow") :
argument is not numeric or logical: returning NA
2: In mean.default("Independent Value", "Sapflow") :
argument is not numeric or logical: returning NA
3: In mean.default("Independent Value", "Sapflow") :
argument is not numeric or logical: returning NA
cast()
library(reshape)
Sapflow.Summary<-cast(Sapflow_new,
Date~Independent_Variable, mean,
value=c('Independent_Value','Sapflow'))
This output is very close to my goal but mean sapflow is missing and the months contained in the "Date" are organised in the wrong order because the arrangment of my code is probably incorrect.
Date Humidity Radiation Temperature
1 August 18.38968 178.9806 71.73355
2 July 21.80065 270.9065 61.33065
3 June 17.60733 263.6733 70.56133
4 October 11.34867 93.6000 81.74300
5 September 14.82200 152.2333 72.21367
Data:
structure(list(Date = structure(c(3L, 3L, 3L, 3L, 3L,
3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L,
1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L,
5L, 5L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L,
4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L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 3L, 3L, 3L, 3L, 3L,
3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Humidity",
"Radiation",
"Temperature"), class = "factor"), Independent_Value =
c(19.67,
18.82, 20.38, 14.94, 12.92, 15.28, 15.12, 16.05, 15.19,
16.67,
18.69, 14.61, 16.71, 17.35, 16.98, 15.44, 15.21, 18.62,
20.11,
18.64, 15.66, 17.2, 18.21, 19.32, 23.02, 21.69, 18.03,
18.46,
18.45, 20.78, 23.04, 22.05, 19.71, 20.59, 24.89, 23.34,
24.7,
24.2, 22.43, 18.21, 17.66, 18.23, 20.36, 22.83, 23.52,
22.88,
19.59, 21.51, 22.25, 21.47, 22.03, 22.51, 25.54, 24.01,
24.28,
26.21, 23.72, 17.63, 17.27, 19.19, 19.97, 19.84, 22.78,
24.46,
23.05, 23.31, 24.75, 23.23, 18.91, 15.56, 13.51, 15.8,
17.67,
19.18, 18.93, 20.05, 17.1, 16.87, 18.77, 20.49, 21.5,
18.04,
18.82, 17.38, 13.05, 13.13, 13.48, 16.32, 16.74, 16.11,
15.77,
15.48, 18.17, 18.16, 18.44, 16.63, 16.64, 14.47, 13.07,
14.14,
17.27, 16.71, 18.22, 12.9, 13.95, 14.7, 15.78, 17.52,
19.66,
18.87, 18.07, 16.4, 12.92, 10.57, 10.04, 9.78, 10.24,
14.25,
15.92, 11.59, 9.25, 10.33, 11.22, 15.03, 13.67, 14.26,
15.42,
8.34, 8.56, 12.37, 14.38, 15.47, 16.4, 17.15, 20.05,
11.08, 10.63,
14.34, 13.27, 9.33, 8.1, 10.95, 12.79, 8.64, 11.42,
12.12, 9.91,
7.86, 3.51, 4.97, 3.63, 5.59, 85.07, 79.72, 72.83, 90.1,
83.02,
73.34, 77.11, 74.79, 81.66, 77.71, 66.14, 78.15, 69.33,
68.13,
60.31, 69.47, 81.86, 78.63, 77.69, 77.56, 52.88, 53.32,
53.74,
55.85, 49.56, 55.3, 69.25, 74.96, 69.29, 60.07, 54.31,
48.6,
55.73, 56.74, 47.66, 60.51, 55.64, 58.39, 63.8, 63.16,
73.65,
71.08, 64.34, 60.1, 51.61, 54.87, 58.23, 52.49, 52.56,
59.64,
67.85, 64.42, 60.08, 59.71, 57.12, 58.7, 68.85, 72.44,
89.13,
77.67, 62.17, 61.3, 63.58, 66.26, 60.09, 56.63, 53.11,
59.84,
60.06, 80.76, 79.51, 73.96, 84.58, 78.77, 71.65, 72.59,
77.52,
69.04, 78.26, 77.22, 73.75, 81.95, 82.04, 78.14, 73.41,
72.76,
90.68, 74.24, 71.3, 74.4, 60.26, 66.08, 65.18, 57.17,
66.88,
75.53, 71.52, 74.97, 66.02, 78.06, 73.58, 68.18, 83.55,
80.4,
66.28, 72.32, 72.39, 77.74, 69.81, 74.21, 77.37, 88.28,
65.33,
87.54, 80.49, 69.58, 68.18, 69.25, 60.06, 66.38, 68.51,
71.65,
63.29, 76.63, 80.46, 85.56, 81.25, 94.48, 73.87, 76.8,
72.83,
77.55, 81.5, 77.7, 75.79, 94.38, 99.55, 94.14, 87.29,
84.81,
82.63, 85.27, 84.52, 71.13, 76.28, 78.06, 82.83, 75.18,
83.8,
85.38, 84, 85.33, 197.8, 195.5, 288, 72, 160.5, 337.1,
176.9,
242.3, 189.4, 295.7, 363.2, 158, 290, 251.2, 297.3,
192.6, 163.5,
274.5, 210.7, 243.4, 287.4, 375.7, 290.5, 336.4, 361.6,
369.2,
302.6, 295.2, 348.5, 343.5, 327.6, 358.9, 358.6, 288.9,
325.6,
307.8, 321.3, 321.5, 280.6, 264.9, 253, 279.5, 318.1,
285.1,
330.8, 252, 201, 229.9, 259.3, 230.4, 265.5, 214.1, 307,
311.1,
282.5, 256.9, 227.2, 263.4, 68.2, 130.8, 276.6, 299.2,
276.5,
243.9, 291, 289.3, 290.6, 259.6, 220.5, 72.7, 158.9,
233.8, 105.9,
164.2, 168.1, 188.7, 120.1, 217.7, 111.2, 114.7, 143.6,
55.2,
108.5, 162.2, 185, 197.7, 54.1, 126.3, 111.2, 135.4,
228.3, 214.3,
240.1, 247.6, 173, 172.4, 131.9, 149.4, 203.1, 92.3,
168.5, 146.6,
65.9, 103.6, 200.2, 131.3, 183.5, 128.3, 140.6, 124.1,
125.9,
75.8, 173.2, 47.9, 111.7, 205.8, 188.3, 175.6, 193.7,
170.4,
188.3, 108, 171.1, 59.5, 87.7, 142.2, 111.8, 26.3,
129.9, 103.1,
158.7, 147.9, 109.8, 67.8, 106.6, 12.3, 15.8, 53, 63.4,
86.2,
123.3, 112.9, 128.2, 141.9, 81.6, 102, 86.8, 83.9, 50,
96.8,
100.5, 47), Sapflow = c(14.97, 16.31, 17.52, 7.45,
12.18, 15.82,
11.79, 14.45, 10.95, 13.62, 16.28, 11.42, 16.13, 15.09,
17.28,
14.43, 11.7, 16.06, 17.66, 16.33, 17.79, 18.58, 19.41,
19.8,
21.63, 21.35, 17.81, 17.56, 19.37, 21.27, 23.26, 23.67,
22.64,
21.85, 24.81, 22.36, 24.72, 23.87, 23.67, 22.01, 19.23,
19.92,
21.99, 23.6, 24.9, 24.46, 22.22, 23.95, 24.81, 23.88,
22.98,
24.47, 26.09, 25.97, 25.82, 26.24, 25.09, 22, 16.91,
21.35, 25.32,
25.76, 26.38, 25.78, 25.77, 25.15, 26.29, 26.22,
24.59, 18.26,
18.91, 21.57, 21.37, 21.29, 23.96, 24.85, 21.02, 23.05,
22.69,
23.9, 25.24, 25.4, 23.19, 22.8, 22.08, 21.86, 13.82,
22.05, 23.21,
20.12, 22.73, 21.88, 23.33, 24.76, 23.5, 22.06, 22.01,
20.65,
21.54, 19.9, 21.67, 21.84, 18.82, 17.99, 21.41, 23.53,
23.39,
25.75, 22.62, 22.25, 21.81, 16.81, 20.42, 12.08, 12.36,
15.31,
14.14, 15.48, 15.18, 14.19, 12.09, 12.39, 12.34, 12.61,
10.79,
10.53, 11.29, 9.92, 9.79, 10.86, 10.98, 10.58, 12.54,
12.52,
12.25, 6.38, 0.91, 5.24, 6.56, 5.72, 4.55, 4.99, 2.88,
0.99,
1.03, 1.57, 2.07, 2.3, 2.22, 2.11, 2.21, 2.29, 14.97,
16.31,
17.52, 7.45, 12.18, 15.82, 11.79, 14.45, 10.95, 13.62,
16.28,
11.42, 16.13, 15.09, 17.28, 14.43, 11.7, 16.06, 17.66,
16.33,
17.79, 18.58, 19.41, 19.8, 21.63, 21.35, 17.81, 17.56,
19.37,
21.27, 23.26, 23.67, 22.64, 21.85, 24.81, 22.36,
24.72, 23.87,
23.67, 22.01, 19.23, 19.92, 21.99, 23.6, 24.9, 24.46,
22.22,
23.95, 24.81, 23.88, 22.98, 24.47, 26.09, 25.97, 25.82,
26.24,
25.09, 22, 16.91, 21.35, 25.32, 25.76, 26.38, 25.78,
25.77, 25.15,
26.29, 26.22, 24.59, 18.26, 18.91, 21.57, 21.37, 21.29,
23.96,
24.85, 21.02, 23.05, 22.69, 23.9, 25.24, 25.4, 23.19,
22.8, 22.08,
21.86, 13.82, 22.05, 23.21, 20.12, 22.73, 21.88, 23.33,
24.76,
23.5, 22.06, 22.01, 20.65, 21.54, 19.9, 21.67, 21.84,
18.82,
17.99, 21.41, 23.53, 23.39, 25.75, 22.62, 22.25, 21.81,
16.81,
20.42, 12.08, 12.36, 15.31, 14.14, 15.48, 15.18, 14.19,
12.09,
12.39, 12.34, 12.61, 10.79, 10.53, 11.29, 9.92, 9.79,
10.86,
10.98, 10.58, 12.54, 12.52, 12.25, 6.38, 0.91, 5.24,
6.56, 5.72,
4.55, 4.99, 2.88, 0.99, 1.03, 1.57, 2.07, 2.3, 2.22,
2.11, 2.21,
2.29, 14.97, 16.31, 17.52, 7.45, 12.18, 15.82, 11.79,
14.45,
10.95, 13.62, 16.28, 11.42, 16.13, 15.09, 17.28, 14.43,
11.7,
16.06, 17.66, 16.33, 17.79, 18.58, 19.41, 19.8, 21.63,
21.35,
17.81, 17.56, 19.37, 21.27, 23.26, 23.67, 22.64, 21.85,
24.81,
22.36, 24.72, 23.87, 23.67, 22.01, 19.23, 19.92, 21.99,
23.6,
24.9, 24.46, 22.22, 23.95, 24.81, 23.88, 22.98, 24.47,
26.09,
25.97, 25.82, 26.24, 25.09, 22, 16.91, 21.35, 25.32,
25.76, 26.38,
25.78, 25.77, 25.15, 26.29, 26.22, 24.59, 18.26, 18.91,
21.57,
21.37, 21.29, 23.96, 24.85, 21.02, 23.05, 22.69, 23.9,
25.24,
25.4, 23.19, 22.8, 22.08, 21.86, 13.82, 22.05, 23.21,
20.12,
22.73, 21.88, 23.33, 24.76, 23.5, 22.06, 22.01, 20.65,
21.54,
19.9, 21.67, 21.84, 18.82, 17.99, 21.41, 23.53, 23.39,
25.75,
22.62, 22.25, 21.81, 16.81, 20.42, 12.08, 12.36, 15.31,
14.14,
15.48, 15.18, 14.19, 12.09, 12.39, 12.34, 12.61, 10.79,
10.53,
11.29, 9.92, 9.79, 10.86, 10.98, 10.58, 12.54, 12.52,
12.25,
6.38, 0.91, 5.24, 6.56, 5.72, 4.55, 4.99, 2.88, 0.99,
1.03, 1.57,
2.07, 2.3, 2.22, 2.11, 2.21, 2.29)), class =
"data.frame", row.names = c(NA,
-456L))
It is not a ddply() or a cast() solution, but using tidyverse and reshape2 you can do:
df %>%
group_by(Date, Independent_Variable) %>%
summarise(Independent_Value = mean(Independent_Value)) %>%
mutate(Independent_Variable = paste(Independent_Variable, "IV", sep = "_")) %>%
dcast(Date~Independent_Variable, value.var = "Independent_Value") %>%
arrange(factor(Date, levels = month.name)) %>%
left_join(df %>%
group_by(Date, Independent_Variable) %>%
summarise(Sapflow = mean(Sapflow)) %>%
mutate(Independent_Variable = paste(Independent_Variable, "Sapflow", sep = "_")) %>%
dcast(Date~Independent_Variable, value.var = "Sapflow") %>%
arrange(factor(Date, levels = month.name)),
by = c("Date" = "Date"))
Date Humidity_IV Radiation_IV Temperature_IV Humidity_Sapflow
1 June 17.60733 263.6733 70.56133 16.067000
2 July 21.80065 270.9065 61.33065 23.356774
3 August 18.38968 178.9806 71.73355 22.941613
4 September 14.82200 152.2333 72.21367 19.309333
5 October 11.34867 93.6000 81.74300 6.700667
Radiation_Sapflow Temperature_Sapflow
1 16.067000 16.067000
2 23.356774 23.356774
3 22.941613 22.941613
4 19.309333 19.309333
5 6.700667 6.700667
First, it is grouping by "Date" and "Independent_Variable" and summarising "Independent_Value". Second, it is adding "_IV" to the values in Independent_Variable. Third, it is reshaping the data and arranging according the real order of months. Fourth, it is doing the first three steps for "Sapflow". Finally, it is merging the two.
Or by using just tidyverse:
df %>%
group_by(Date, Independent_Variable) %>% #Grouping
summarise_all(funs(mean = mean(.))) %>% #Summarising all variables and adding "_mean" to the new variables
arrange(factor(Date, levels = month.name)) #Arranging according the real order of months
Date Independent_Variable Independent_Value_mean Sapflow_mean
<fct> <fct> <dbl> <dbl>
1 June Humidity 17.6 16.1
2 June Radiation 264. 16.1
3 June Temperature 70.6 16.1
4 July Humidity 21.8 23.4
5 July Radiation 271. 23.4
6 July Temperature 61.3 23.4

Resources