Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个大型 ICD-10 数据,我想创建子组并从中获取总和。
例如,我有“JAL01、JAL20 和 JAL21”,我需要以“JAL”开头的所有代码的总和。我怎么做?
子串前 3 个字母,然后分组和求和:
# example data df1 <- data.frame(icd = c("JAL01", "JAL20", "JAL21", "foo11", "foo22"), x = 1:5) # get 1st 3 letters df1$grp <- substr(df1$icd, 1, 3) # get sum per group aggregate(x ~ grp, df1, sum) # grp x # 1 foo 9 # 2 JAL 6