I have the following dataset:
structure(list(Patient_ID = c("1234", "1234", "1234", "1234",
"1234", "1234", "1234", "1234", "1234"), Unit_Type = c("ABC",
"ABC", "ABC", "ABC", "ABC", "DEF", "DEF", "DEF", "GHI"), Status = c("Returned",
"Returned", "Returned", "Returned", "Transfused", "Transfused",
"Transfused", "Transfused", "Transfused")), class = "data.frame", row.names = c(NA,
-9L))
and have used the following calculation on it:
df <- df %>%
count(Patient_ID, Unit_Type, Status) %>%
pivot_wider(names_from = c(Unit_Type, Status), values_from = n)
I want to sum 'ABC_Returned' and 'ABC_Transfused' by Patient_ID (I know the example dataset only has one unique patient ID, but my real dataset has many more), but I keep getting the following error message:
> aggregate(df, by=list(df$ABC_Transfused, df$ABC_Returned), FUN=sum, na.rm = TRUE)
Error in FUN(X[[i]], ...) : invalid 'type' (character) of argument
Anyone know how to bypass this? Ideally, I would like to create a new column called ABC_Ordered That is the sum of 'ABC_Returned' and 'ABC_Transfused', grouped by Patient ID.