我想比较来自两个不同索赔付款人的CPT代码的费用。两者都有标准和非标准价格的供应商。我使用的是dplyr
和modeest::mlv
,但它没有按预期进行。下面是一些样本数据;
source CPTCode ParNonPar Key net_paid PaidFreq seq
ABC 100 Y ABC100Y -341.00 6 1
ABC 100 Y ABC100Y 0.00 2 2
ABC 100 Y ABC100Y 341.00 6 3
XYZ 103 Y XYZ103Y 740.28 1 1
XYZ 104 N XYZ104N 0.00 2 1
XYZ 104 N XYZ104N 401.82 1 2
XYZ 104 N XYZ104N 726.18 1 3
XYZ 104 N XYZ104N 893.00 1 4
XYZ 104 N XYZ104N 928.20 2 5
XYZ 104 N XYZ104N 940.00 2 6
和代码
str(data)
View(data)
## Expand frequency count to individual observations
n.times <- data$PaidAmounts
dataObs <- data[rep(seq_len(nrow(data)), n.times),]
## Calculate mean for each CPTCode (for mode use modeest library)
library(dplyr)
library(modeest)
dataSummary <- dataObs %>%
group_by(ParNonPar, CPTCode) %>%
summarise(mean = mean(net_paid),
median=median(net_paid),
mode = mlv(net_paid, method=mfv),
total = sum(net_paid))
str(dataSummary)
我认为我可以使用均值和中位数在总结函数中加载modeest,但这个公式错误 as.character(x)出错: 不能强迫类型&#39;关闭&#39;对于类型&#39;字符&#39; 没有mlv我得到这样的df,但我想要的是在一条线上获得付款人cpt的所有统计数据。我设想通过限制x和y段在箱图中绘制图形,一旦我得到我需要的行
答案不充分(我忘了在这里得到付款人姓名!)
ParNonPar CPTCode mean median(net_paid) total
N 0513F 0.000000 0.000 0.00
N 0518F 0.000000 0.000 0.00
N 10022 0.000000 0.000 0.00
N 10060 73.660000 90.120 294.64
N 10061 324.575000 340.500 1298.30
N 10081 312.000000 312.000 312.00
thanks very much for your time and effort.
答案 0 :(得分:7)
您需要对代码进行一些更改才能使mlv正常工作。
尝试:
dataSummary <- dataObs %>%
group_by(ParNonPar, CPTCode) %>%
summarise(mean = mean(net_paid),
meadian=median(net_paid),
mode = mlv(net_paid, method='mfv')[['M']],
total = sum(net_paid))
得到:
> dataSummary
Source: local data frame [3 x 6]
Groups: ParNonPar
ParNonPar CPTCode mean meadian mode total
1 N 104 639.7111 893.00 622.7333 5757.40
2 Y 100 0.0000 0.00 0.0000 0.00
3 Y 103 740.2800 740.28 740.2800 740.28
希望能帮助你前进。
答案 1 :(得分:0)
我使用这种方法:
df <- data.frame(groups = c("A", "A", "A", "B", "B", "C", "C", "C", "D"), nums = c("1", "2", "1", "2", "3", "4", "5", "5", "1"))
如下所示:
groups nums
A 1
A 2
A 1
B 2
B 3
C 4
C 5
C 5
D 1
然后我定义:
mode <- function(codes){
which.max(tabulate(codes))
}
并执行以下操作:
mds <- df %>%
group_by(groups) %>%
summarise(mode = mode(nums))
给予:
groups mode
A 1
B 2
C 5
D 1