我们可以通过‘Location’得到数字的sum
,然后通过划分Total列来创建Proportion列
library(dplyr) # version >= 1.1.0
library(stringr)
df1 %>%
reframe(across(everything(), ~ sum(.x, na.rm = TRUE),
.names = "Total_{str_remove(.col, ' .*')}"), .by = "Location") %>%
mutate(proportion = round(Total_CGM/Total_Diabetes, 2))
-输出
Location Total_Diabetes Total_CGM proportion
1 CA 2 1 0.50
2 TX 3 1 0.33
3 AZ 3 2 0.67
或使用base R
transform(aggregate(.~ Location, df1, sum),
proportion = round(`CGM prescribed`/`Diabetes present`, 2),
check.names = FALSE)
-输出
Location Diabetes present CGM prescribed proportion
1 AZ 3 2 0.67
2 CA 2 1 0.50
3 TX 3 1 0.33
数据
df1 <- structure(list(Location = c("CA", "TX", "TX", "CA", "AZ", "AZ",
"AZ", "TX"), `Diabetes present` = c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), `CGM prescribed` = c(1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L)),
class = "数据.frame", row.names = c(NA,
-8L))