r - dplyr::summarise 根据另一列 max 拉取值
问题描述
基于以下可重现的代码,如何根据以下Address
条件有条件地添加列max(LeastNEmployees)
:
dat_url <- "https://gender-pay-gap.service.gov.uk/viewing/download-data/2019"
dat <- read_csv(dat_url)
#2 convert EmployerSize
df = data.frame(EmployerSize=c('Less than 250','250 to 499', '500 to 999', '1000 to 4999', '5000 to 19,999', '20,000 or more'),
LeastNEmployees = c(1,250,500, 1000, 5000, 20000))
a <- dat %>%
left_join(df, c('EmployerSize' = 'EmployerSize')) %>%
group_by(ResponsiblePerson) %>%
summarize(
across(where(is.numeric) & !starts_with("Least"), mean),
across(c("EmployerName","SicCodes"), ~toString(.x)),
LeastNEmployees = max(LeastNEmployees))
解决方案
这是which
有条件的。
a <- dat %>%
left_join(df, c('EmployerSize' = 'EmployerSize')) %>%
group_by(ResponsiblePerson) %>%
summarize(
across(where(is.numeric) & !starts_with("Least"), mean),
across(c("EmployerName","SicCodes"), ~toString(.x)),
LeastNEmployees = max(LeastNEmployees),
Address = Address[which(LeastNEmployees == max(LeastNEmployees))])
推荐阅读
- javascript - 搜索输入正在取消文本作为 vuejs 中的一种类型
- azure-active-directory - Azure Active Directory:如果分配给管理单元,“用户管理员”不能删除用户
- python - 如何使用 ipyvuetify 的 Slider 对象中的插槽?
- firebase - Firebase 部署
- amazon-web-services - 关于 AWS Lambda 函数在触发警报后重复触发 SNS 函数
- electron - 如何在 Electron 中使用 Redux 开发工具?
- arrays - 将 product line_items 存储在与供应商的关联数组中
- sql - 使用别名的 SQL 中的多个联接
- google-sheets - 如何在 Google 表格中溢出数字和日期?
- sql-server - SQL Server 作业保持在 run_status = 4