我有以下示例数据:
# data
school = c('ABC University','ABC Uni','DFG University','DFG U')
applicant = c(2000,3100,210,2000)
students = c(100,2000,300,4000)
df = data.frame(school,applicant,students)
我想合并到这个:
|school |appliant| students |
-----------------------------------
|ABC University| 5100 | 2100 |
|DFG University| 2210 | 4300 |
我运行了这段代码:
df$school[df$school == 'ABC Uni'] = 'ABC University'
但它给了我两次 ABC 大学,而不是将它们合并在一起。
最佳答案
这实际上取决于其余的字符串,但您可以查看 grep
并使用 ^
开始。
df[grep('^ABC U', df$school), 'school'] <- 'ABC University'
df[grep('^DFG U', df$school), 'school'] <- 'DFG University'
和往常一样聚合
。
aggregate(cbind(applicant, students) ~ school, df, sum)
# school applicant students
# 1 ABC University 5100 2100
# 2 DFG University 2210 4300
关于r - 根据条件合并 R 中的行和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68532625/