I need to calculate proportional use of treatments within each trial. However, my code produces incorrect values. Also, I have a few trials where treatments were not used (e.g. Trial 7 - Control) that are populating with Nan. How can I input a value of 0 for trials that were not used. Output pictured below. Thanks
[![enter image description here][1]][1]
filter(Use==1) %>%
group_by(Trial,Treatment,Use, .drop=F) %>%
summarise(trial_n=n()) %>%
mutate(Prop = n / trial_n * 100)```
`structure(list(DateTime = structure(c(1596994200, 1596994200,
1596994200, 1596994200, 1596994200, 1596994200, 1596994200,
1596994200,
1596995100, 1596995100), class = c("POSIXct", "POSIXt"), tzone = ""),
Treatment = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 1L,
1L), .Label = c("30%Shade", "60%Shade", "90%Shade", "Control"
), class = "factor"), Pyranometer = structure(c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_), .Label = c("1",
"2", "3", "4"), class = "factor"), Irradiance = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_), Total_Solar_Flux = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_), Date = structure(c(18483,
18483, 18483, 18483, 18483, 18483, 18483, 18483, 18483, 18483
), class = "Date"), Time = c("12:30:00", "12:30:00", "12:30:00",
"12:30:00", "12:30:00", "12:30:00", "12:30:00", "12:30:00",
"12:45:00", "12:45:00"), Year = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", "2"), class = "factor"),
Trial = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14"), class = "factor"), Id = c("Dianne",
"June", "Dianne", "June", "Dianne", "June", "Dianne", "June",
"Dianne", "June"), Ambient_F = c(95.8, 95.8, 95.8, 95.8,
95.8, 95.8, 95.8, 95.8, 96.6, 96.6), BGtemp_F = c(112.5,
112.5, 106.2, 106.2, 99.7, 99.7, 118.4, 118.4, 113.4, 113.4
), Use = c(1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L), Ambient_C =
c(35.44,
35.44, 35.44, 35.44, 35.44, 35.44, 35.44, 35.44, 35.89, 35.89
), BGtemp_C = c(44.72, 44.72, 41.22, 41.22, 37.61, 37.61,
48, 48, 45.22, 45.22)), row.names = c(NA, 10L), class =
"data.frame")`
[1]: https://i.stack.imgur.com/gP3Nk.png