R语言中如何修复数据框内ID对应amount字段的异常值?
问题与解决方案
问题背景
处理R语言中的data frame时,ID为人员编码,同一ID的amount字段在持有期内应保持一致,但存在部分四舍五入异常值,需修正为该ID下的正确值。
原始数据
| ID | period | amount | share |
|---|---|---|---|
| 1x | 202001 | 99.87 | 9.87 |
| 1x | 202002 | 99.87 | 9.87 |
| 1x | 202003 | 99.87 | 9.87 |
| 1x | 202004 | 100 | 9.87 |
| 1x | 202005 | 99.87 | 9.87 |
| 1x | 202006 | 99.87 | 9.87 |
| 2x | 202105 | 111.11 | 11.11 |
| 2x | 202106 | 111.11 | 11.11 |
| 2x | 202107 | 110 | 11.11 |
| 2x | 202108 | 111.11 | 11.11 |
| 2x | 202109 | 111.11 | 11.11 |
| 2x | 202110 | 111.11 | 11.11 |
目标数据
| ID | period | amount | share |
|---|---|---|---|
| 1x | 202001 | 99.87 | 9.87 |
| 1x | 202002 | 99.87 | 9.87 |
| 1x | 202003 | 99.87 | 9.87 |
| 1x | 202004 | 99.87 | 9.87 |
| 1x | 202005 | 99.87 | 9.87 |
| 1x | 202006 | 99.87 | 9.87 |
| 2x | 202105 | 111.11 | 11.11 |
| 2x | 202106 | 111.11 | 11.11 |
| 2x | 202107 | 111.11 | 11.11 |
| 2x | 202108 | 111.11 | 11.11 |
| 2x | 202109 | 111.11 | 11.11 |
| 2x | 202110 | 111.11 | 11.11 |
解决方案
方法1:基于众数修正(通用场景)
当仅知道同一ID下amount应一致,无明确业务逻辑时,用每个ID下出现次数最多的amount(众数)替换异常值:
# 加载dplyr包 library(dplyr) # 构造原始数据 df <- data.frame( ID = c(rep("1x",6), rep("2x",6)), period = c(202001:202006, 202105:202110), amount = c(99.87,99.87,99.87,100,99.87,99.87,111.11,111.11,110,111.11,111.11,111.11), share = c(rep(9.87,6), rep(11.11,6)) ) # 修正数据 df_fixed <- df %>% group_by(ID) %>% mutate( # 获取当前ID下的众数作为正确值 correct_amount = as.numeric(names(sort(table(amount), decreasing = TRUE)[1])), # 标记异常值(可选) is_anomaly = amount != correct_amount, # 替换异常值 amount = correct_amount ) %>% ungroup() %>% # 移除辅助列(可选) select(-correct_amount, -is_anomaly) # 查看结果 print(df_fixed)
方法2:基于业务逻辑修正(更精准)
从示例数据可看出amount = share * 10,若存在此类明确业务规则,直接通过计算修正:
df_fixed <- df %>% mutate( # 基于share计算正确的amount correct_amount = share * 10, # 标记异常值(可选) is_anomaly = amount != correct_amount, # 替换异常值 amount = correct_amount ) %>% select(-correct_amount, -is_anomaly) print(df_fixed)
内容的提问来源于stack exchange,提问作者Facundo Javier Vargas
相关产品推荐
相关产品推荐

