I am attempting to:
- calculate the difference in call duration between police units responding to the same call
- identify the longest duration among a group of calls with the same call ID
- arrange in descending order of duration
My steps to do so are found in the code snippets below.
First, I arrange in descending order by ID (multiple calls with the same ID) and then arrange within that by the call duration in hours (descending).
Then, I make my data.frame into a data.table.
Then, apply sequences (descending) by duration.
call_duration_diff_by_unit[, duration_seq := seq(CALL_DURATION_HOURS), by = c("ID")]
This is where the problem occurs: I get an error that says
"Error in
[.data.table(call_duration_diff_by_unit, ,:=(duration_seq, : Supplied 2 items to be assigned to group 1 of size 1 in column 'duration_seq'. The RHS length must either be 1 (single values are ok) or match the LHS length exactly. If you wish to 'recycle' the RHS please use rep() explicitly to make this intent clear to readers of your code."
The only explanation for this error I have found was specific to a unique package that I am not using. I understand the concept of "recycling" now, but not sure how it applies to this scenario... there aren't two vectors with different lengths.
Could R be reading the by = c("ID") part incorrectly as a second input?
call_duration_diff_by_unit <- cad_cfs_data %>%
arrange(desc(ID), desc(CALL_DURATION_HOURS))
call_duration_diff_by_unit <-
data.table(call_duration_diff_by_unit)
call_duration_diff_by_unit[, duration_seq := seq(CALL_DURATION_HOURS), by = c("ID")]
I expected it to make a unique numeric ID (assigning 1 to the longest duration) for each group of unique call IDs. Instead, I get the error and it doesn't save the variable "duration_seq" for use later down in the code.
IDgroup (i.e. the number of rows for whichIDis equal to the given value), and the output of the RHS of:=. - IceCreamToucanseq_alonginstead ofseq- IceCreamToucan