This article explains the cause and solution for the must be size 1, not 0 error that can occur in some groups when using a custom R command to calculate “the difference in values between specific year-months” for each group.
For example, suppose you were using an R script like the following to calculate the difference between the 2025-04 and 2024-10 measurements for each group:
group_by(`sensor_id`) %>%
mutate(`value_diff(2025-04 minus 2024-10)` =
`measured_value`[`year_month` == "2025-04"] -
`measured_value`[`year_month` == "2024-10"]) %>%
ungroup()
When you run this, you may encounter an error like the one below:
! `value_diff(2025-04 minus 2024-10)` must be size 1, not 0.
ℹ In group 4: `sensor_id = "S004"`.
The cause of this error is that the group that triggered the error (sensor_id = “S004”) has no rows matching year_month == "2025-04" or year_month == "2024-10".
A filtering expression like [year_month == "2025-04"] returns a result of “length 0” when there are no matching rows. Because mutate expects a value of length 1 for each row in a group, a length-0 result produces the must be size 1, not 0 error.
This is especially likely to happen when only some groups are missing data for the relevant year-months.
As a solution, wrap each value used in the difference calculation with first():
group_by(`sensor_id`) %>%
mutate(`value_diff(2025-04 minus 2024-10)` =
first(`measured_value`[`year_month` == "2025-04"]) -
first(`measured_value`[`year_month` == "2024-10"])) %>%
ungroup()
For groups with no matching rows, first() returns a missing value (NA). This ensures the value is always length 1, so the script runs without error even when some groups have incomplete data.

