【发布时间】:2022-09-29 19:56:43
【问题描述】:
我有以下数据集:
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\",
\"R\", \"R\", \"R\", \"T\", \"T\",
\"T\", \"T\", \"T\")), class = \"data.frame\", row.names = c(NA,
-9L))
并对其使用了以下计算:
df <- df %>%
count(ID, Unit_Type, Status) %>%
pivot_wider(names_from = c(Unit, Status), values_from = n)
我想将\'ABC_R\' 和\'ABC_T\' 与ID 相加(我知道示例数据集只有一个唯一的患者ID,但我的真实数据集有更多),但我不断收到以下错误消息:
> aggregate(df, by=list(df$ABC_T, df$ABC_R), FUN=sum, na.rm = TRUE)
Error in FUN(X[[i]], ...) : invalid \'type\' (character) of argument
-
当然,它的结构(list(Patient_ID = \"1234\", ABC_Returned = 4L, ABC_Transfused = 1L, DEF_Transfused = 3L, GHI_Transfused = 1L, ABC_Ordered = 5), row.names = c(NA, -1L), class= c(\"tbl_df\", \"tbl\", \"data.frame\"))
标签: r