我正在尝试在以下末尾添加一个名为“类别”的列。我想要的输入将包括 mutate 语句的内容。
Combined States Institution Number of Students Percent of Students Category
AL Stillman College 74 15.78% ????
我在撰写声明时遇到困难。我需要再做一次 mutate 语句吗?我写的内容如下:
Impact_Stats_Main_Top_10_Schools_Summary <- Awards %>%
select(`Combined_State`, `Institution`, `Student Name`, `Ranking`, `HBCU`, `UNCF_Member`) %>%
mutate (`Category` = case_when (`Ranking` =='1' | HBCU == TRUE & UNCF_Member == TRUE ~ "1 – UNCF",
`Ranking` == '2' | HBCU == TRUE & UNCF_Member == FALSE ~ "2 – HBCU",
`Ranking` == '3' | HBCU == FALSE & UNCF_Member == FALSE ~ "3 – Other"
)) %>%
group_by (`Combined_State`, `Institution`) %>%
summarize(`Number of Students` = n_distinct (`Student Name`)) %>%
mutate(`Percent of Students` = scales::label_percent()(`Number of Students`/sum(`Number of Students`))) %>%
arrange(Combined_State,desc(`Number of Students`)) %>%
slice_max(`Number of Students`, n = 10) %>%
filter(`Number of Students` > 1)
我尝试添加另一个 mutate 语句来显示名为 CATEGORY 的列。
您也许可以创建类别,然后单独进行计数,然后进行内部联接,如下所示:
inner_join(
Awards %>%
mutate (`Category` = case_when (
`Ranking` =='1' | HBCU == TRUE & UNCF_Member == TRUE ~ "1 – UNCF",
`Ranking` == '2' | HBCU == TRUE & UNCF_Member == FALSE ~ "2 – HBCU",
`Ranking` == '3' | HBCU == FALSE & UNCF_Member == FALSE ~ "3 – Other"
)) %>%
select(`Combined_State`, `Institution`,`Category`) %>%
distinct(),
Awards %>%
group_by (`Combined_State`, `Institution`) %>%
summarize(`Number of Students` = n_distinct (`Student Name`), ) %>%
mutate(`Percent of Students` = scales::label_percent()(`Number of Students`/sum(`Number of Students`))) %>%
arrange(Combined_State,desc(`Number of Students`)) %>%
slice_max(`Number of Students`, n = 10) %>%
filter(`Number of Students` > 1),
by = c("Combined_State", "Institution")
)
这可能不是最好的方法。您的问题可能会受益于我们可以使用的一些示例数据。