如何在R中总结一个组的模式 [英] How to get the mode of a group in summarize in R

查看:8
本文介绍了如何在R中总结一个组的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较来自两个不同索赔付款人的 CPT 代码的成本.两者都有平价和非平价供应商.我正在使用 dplyrmodeest::mlv,但它没有按预期工作.这是一些示例数据;

I want to compare costs of CPT codes from two different claims payers. Both have par and non par priced providers. I am using dplyr and modeest::mlv, but its not working out as anticipated. Heres some sample data;

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)                     

我以为我可以用均值和中位数在汇总函数中加载适度的,但是这个公式错误as.character(x) 中的错误:不能将闭包"类型强制转换为字符"类型的向量如果没有 mlv,我会得到这样的 df,但我想要的是在一行上获取付款人 cpt 的所有统计数据.我设想通过限制 x 和 y 段在箱线图中绘制它,一旦我在一行中得到我需要的东西

I thought I could load modeest in the summarize function with the mean and median, but this formulation errors out with Error in as.character(x) : cannot coerce type 'closure' to vector of type 'character' Without mlv I am getting a df like this, but what I want is to get all the stats for a payer cpt on one line. I envision graphing it in boxplots by limiting the x and y segments, once I get what I need on a row

不恰当的答案是这个(我忘记在此处输入付款人姓名!)

the inadequate answer is this ( I forgot to get the payer name in here!)

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.

推荐答案

您需要对代码进行一些更改才能使 mlv 正常工作.

You need to make a couple of changes to your code for mlv to work.

  1. 方法 (mfv) 必须在引号 ('mfv') 内.这就是导致您出错的原因.
  2. 执行此操作后,由于 mlv 返回一个列表,因此您必须向 summarise() 提供一个值.假设您需要模式 ('M'),则从列表中选择该元素.

试试:

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

希望能帮助您前进.

这篇关于如何在R中总结一个组的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆