如何根据另一个变量的值找到data.frame的最小值

时间:2014-05-22 13:59:24

标签: r dataframe

我有这个数据集

    > head(data,15)
    X        AIC          MSE                    data method num_var
1   0 -8767.4402 0.0001496157 A p=1000 n=1000 SNR=0.5     HF      20
2   1 -8767.4402 0.0001496157 A p=1000 n=1000 SNR=0.5     HM      20
3   2 -2385.3440 0.0913237854 A p=1000 n=1000 SNR=0.5  HMP80       4
4   3 -2340.9765 0.0958494526 A p=1000 n=1000 SNR=0.5  HMP90       2
5   4 -5242.1750 0.0050813667 A p=1000 n=1000 SNR=0.5   LP70      20
6   5 -5242.1750 0.0050813667 A p=1000 n=1000 SNR=0.5   LP85      20
7   6 -8383.1066 0.0002197310 A p=1000 n=1000 SNR=0.5    lCV      20
8   7 -2359.8396 0.0940583853 A p=1000 n=1000 SNR=0.5 rl0270       2
9   8 -2359.8396 0.0940583853 A p=1000 n=1000 SNR=0.5 rl0240       2
10  9 -2359.8396 0.0940583853 A p=1000 n=1000 SNR=0.5 rl0570       2
11 10 -2359.8396 0.0940583853 A p=1000 n=1000 SNR=0.5 rl0540       2
12 11 -2417.3708 0.0886223833 A p=1000 n=1000 SNR=0.5 RL0001       3
13 12 -5242.1750 0.0050813667 A p=1000 n=1000 SNR=0.5   lAIC      20
14 13  -949.9553 0.0039668746  C p=1000 n=200 SNR=0.5     HF      78
15 14  -949.9553 0.0039668746  C p=1000 n=200 SNR=0.5     HM      78
> 

在不同的数据集(数据)中有各种方法及其性能(MSE)。 我想知道每种方法最好的次数。 因此,例如,当数据= A p1000 ....

时,HF可以是最小值

最后,我想要一个数据框,其中每列是一个方法,一行表示该方法最好的次数。

编辑: 建议的代码不起作用

> head(data)
  X       AIC          MSE                    data method num_var
1 0 -8767.440 0.0001496157 A p=1000 n=1000 SNR=0.5     HF      20
2 1 -8767.440 0.0001496157 A p=1000 n=1000 SNR=0.5     HM      20
3 2 -2385.344 0.0913237854 A p=1000 n=1000 SNR=0.5  HMP80       4
4 3 -2340.977 0.0958494526 A p=1000 n=1000 SNR=0.5  HMP90       2
5 4 -5242.175 0.0050813667 A p=1000 n=1000 SNR=0.5   LP70      20
6 5 -5242.175 0.0050813667 A p=1000 n=1000 SNR=0.5   LP85      20
> DT <- as.data.table(data)
> Counts <- DT[, sum(MSE == min(MSE, na.rm=TRUE)), by=method]
> Counts <- setnames(data.table(t(Counts$V1)), Counts$method)
Error in setnames(data.table(t(Counts$V1)), Counts$method) : 
  Passed a vector of type 'integer'. Needs to be type 'character'.
> Counts
    method V1
 1:     HF  1
 2:     HM  1
 3:  HMP80  1
 4:  HMP90  1
 5:   LP70  1
 6:   LP85  1
 7:    lCV  1
 8: rl0270  1
 9: rl0240  1
10: rl0570  1
11: rl0540  1
12: RL0001  1
13:   lAIC  1
> 

1 个答案:

答案 0 :(得分:0)

最简单的方法是转换为data.table

library(data.table)

## Convert to data.table
DT <- as.data.table(DAT)

## Make method into a character
DT[, method := as.character(method)]

## Find the smallest value by method
Counts <- DT[, sum(MSE == min(MSE, na.rm=TRUE)), by=method]

## Convert to wide, if preferred
Counts <- setnames(data.table(t(Counts$V1)), Counts$method)

### RESULTS
    HF HM HMP80 HMP90 LP70 LP85 lCV rl0270 rl0240 rl0570 rl0540 RL0001 lAIC
 1:  1  1     1     1    1    1   1      1      1      1      1      1    1