如何选择满足R中参数的N个资产

时间:2015-01-30 08:41:52

标签: r subset

我想选择其BETAdn列总和为0的4个资产。大矩阵称为ALPHABETA&我运行以下命令来获取满足sum to zero参数的资产子集。

AB <- subset(data.frame((ALPHABETA)), BETAdn+BETAdn >= -0.01 & BETAdn+BETAdn <= .01)

上面的代码返回以下内容:

AB <- structure(list(ALPHAdn = c(0, -2e-04, -2e-04, -0.002, 0.0129, 
-0.0071, 7e-04, -0.0019, -2e-04, -2e-04, -2e-04, -0.0011, -2e-04, 
-2e-04, -2e-04, 0.001, -2e-04, -2e-04, -2e-04, -0.0068, -2e-04, 
-2e-04, -0.0016, -2e-04, -2e-04, -2e-04, -0.0029, 0.002, 3e-04, 
0, -2e-04, -2e-04, -2e-04, -2e-04, -0.0072, 0.0028, -2e-04, -2e-04, 
-2e-04, -2e-04, -0.0025, -0.003, -2e-04, -0.0026, -2e-04, -2e-04, 
-2e-04, -0.002, -2e-04, -2e-04, -2e-04, -2e-04, -2e-04, -2e-04, 
-2e-04, 0.0012, -9e-04, -2e-04, -2e-04, -2e-04, -2e-04, -2e-04, 
-2e-04, -2e-04, 0.0106, -2e-04), BETAup = c(0, 0.7181, 0.2041, 
2.0982, -3.6778, 0.2532, 0.0964, 0.4937, 0.6152, -0.597, -0.1563, 
-0.0014, 0.1627, -0.1742, -0.0131, 0.8372, 0.2327, -0.0019, -0.4405, 
1.3106, 0.44, -1.1557, 0.3253, -0.1607, 0.0171, 0.007, 1.0561, 
0.4863, 0.2045, 0.4859, 0.3233, 0.7361, -0.0787, 0.273, 0.9931, 
1.151, 0.1011, 0.1894, -0.2742, 0.0069, 0.1003, -0.2177, 0.0437, 
0.9002, 0.4438, -0.0501, -0.031, 0.4498, 0.0115, 0.0031, 1e-04, 
-0.0606, 0.0119, 1.4144, -0.0802, -0.004, 0.6779, -1.983, 0.818, 
0.2103, 0.204, 0.4613, 0.058, 0.3685, -1.9407, 1.1002), BETAdn = c(0, 
4e-04, 4e-04, 0.0012, 7e-04, 9e-04, -0.0034, -0.0011, 4e-04, 
4e-04, 4e-04, -0.0027, 4e-04, 4e-04, 4e-04, 0.0033, 4e-04, 7e-04, 
4e-04, 0.0048, 4e-04, 4e-04, -0.0021, 4e-04, 4e-04, 4e-04, -0.0047, 
-0.002, 4e-04, 2e-04, -9e-04, 4e-04, 4e-04, 4e-04, 0.0015, 0.005, 
4e-04, -1e-04, 0.0033, 4e-04, -0.0013, -0.0049, -0.001, 0.0048, 
4e-04, 4e-04, 4e-04, 0.0048, 4e-04, 4e-04, 4e-04, 4e-04, 4e-04, 
4e-04, 4e-04, 0.0046, -0.0023, 4e-04, 4e-04, 4e-04, 4e-04, 4e-04, 
4e-04, 4e-04, 0.0042, 4e-04), ALPHAup = c(0, -0.0107, -0.0029, 
4e-04, 0.027, 0.0137, 2e-04, -0.0016, -0.0043, 0.0084, -3e-04, 
-9e-04, 0.0032, -0.0018, 0.008, 0.0031, -8e-04, 0, 5e-04, 0.0061, 
0.0083, -9e-04, -4e-04, 0.0025, -0.005, 0.0011, 2e-04, -0.006, 
-0.0026, -4e-04, 0.0018, -0.0055, -3e-04, -0.0059, 0.0053, -0.0094, 
-0.002, 0.0015, 0.0021, 1e-04, 0.0048, 0.0101, -0.0015, 0.0068, 
0.0071, 0.0041, -2e-04, -0.0086, -2e-04, -6e-04, -2e-04, 0.0015, 
-0.0017, -0.005, -0.004, -0.0015, 0.0023, 0.0288, 0.0011, -0.0074, 
-0.0041, -0.0092, -0.0011, -0.0066, 0.0137, -0.0124), UPpval = c(NaN, 
0.7812, 0.5756, 0.039, 0.0162, 0.1637, 0.5131, 0.5469, 0.2043, 
0.5301, 0.1703, 0.8713, 0.3905, 0.1556, 0.4194, 0.1435, 0.0175, 
0.1676, 0.0891, 8e-04, 0.1421, 0.1439, 0.4381, 0.5572, 0.6719, 
0.3788, 0, 0.4341, 0.404, 0.1174, 0.6871, 0.0417, 0.1056, 0.9757, 
0.0019, 0.0102, 0.9733, 0.1693, 0.4, 0.1847, 0.6531, 0.385, 0.8424, 
7e-04, 0.2976, 0.6284, 0.2896, 0.926, 0.8516, 0.5919, 0.5761, 
0.837, 0.5985, 1e-04, 0.4427, 0.2718, 0.0776, 0.5231, 0.1577, 
0.4862, 0.9704, 0.9688, 0.9509, 0.8703, 0.2898, 0.1635), DNpval = c(NaN, 
0.5758, 0.324, 0.9876, 0.302, 0.433, 0.9699, 0.9317, 0.6168, 
0.3043, 0.9366, 0.8997, 0.7444, 0.7153, 0.5165, 0.8601, 0.7079, 
0.01, 0.9436, 0.6197, 0.5857, 0.9662, 0.9648, 0.184, 0.7238, 
0.544, 0.9857, 0.3622, 0.3005, 0.9561, 0.9477, 0.3751, 0.8279, 
0.4774, 0.6118, 0.1958, 0.4617, 0.7703, 0.6927, 0.6274, 0.809, 
0.2253, 0.6894, 0.466, 0.7238, 0.6158, 0.8311, 0.3922, 0.4567, 
0.6448, 0.6685, 0.3738, 0.6351, 0.5341, 0.6739, 0.4371, 0.851, 
0.2468, 0.9466, 0.1929, 0.4586, 0.4569, 0.3455, 0.4226, 0.6683, 
0.197)), .Names = c("ALPHAdn", "BETAup", "BETAdn", "ALPHAup", 
"UPpval", "DNpval"), row.names = c("DGS6MO", "FLAGSHIP.EXPRESS.INC", 
"AVESIS.INC", "FRANKLIN.ELECTRONIC.PUBLSHS.INC", "THERAGENICS.CORP", 
"AIRSENSORS.INC", "BANK.OF.EAST.TENNESSEE", "ADTEC.INC", "ORBIS.INC", 
"MAGIC.YEARS.CHILD...LRNG.CTR.INC", "DIGITAL.OPTRONICS.CORP", 
"SPECTRUM.INFORMATION.TECHS.INC", "ENTREE.CORP", "VITEL.FIBER.OPTICS.CORP", 
"ALS.FORMAL.WEAR.INC", "AMERICAN.ECOLOGY.CORP", "AUTHENTICOLOR.INC", 
"COMPARATOR.SYSTEMS.CORP", "DATATAB.INC", "DOUGLAS...LOMASON.CO", 
"GENERAL.BUILDERS.CORP", "EQUIPMENT.COMPANY.AMER", "SAN.CARLOS.MILLING.INC", 
"FORTUNE.NATIONAL.CORP", "KIMCO.ENERGY.CORP", "KREISLER.MANUFACTURING.CORP", 
"RUDDICK.CORP", "NORDSON.CORP", "TANDYCRAFTS.INC", "HUNT.MANUFACTURING.CO", 
"MOUNTAIN.MEDICAL.EQUIP.INC", "ALTEX.INDUSTRIES.INC", "RAND.CAPITAL.CORP", 
"S.F.E.TECHNOLOGIES", "UNO.RESTAURANT.CORP", "HIGH.YIELD.PLUS.FD.INC", 
"TELEPHONE.SPECIALISTS.INC", "PATRIOT.PREMIUM.FD.I", "SAGE.SOFTWARE.INC.INTERSOLV", 
"CITIZENS.SECURITY.GROUP.INC", "GATEWAY.FED.CORP", "ASIAMERICA.EQUITIES.LTD", 
"NORCEN.ENERGY.RESOURCES.LTD", "DUTY.FREE.INTERNATIONAL.INC", 
"CATTLEGUARD.INC", "TIMBERLINE.MINERALS.INC", "SUMMIT.FINANCIAL.CORP", 
"RATHBONE.KING...SEELEY.INC", "INTERNATIONAL.SPECIALTY.PRODUCTS", 
"CHIPPEWA.RESOURCES.CORP", "ADVANCED.PROMOTION.TECHS.INC", "BEVERLY.HILLS.FAN.COMPANY", 
"JAKO.INC", "NEVADA.GOLDFIELDS.CORP", "QUANTUM.VENTURES.GROUP.INC", 
"VINDICATOR.OF.FLORIDA.INC", "T.J.INTERNATIONAL.INC", "KWIK.PRODUCTS.INTERNATIONAL.CORP", 
"BETHLEHEM.RESOURCES.CORP", "TRIAN.EQUITIES.LTD", "CONSOLIDATED.MERCANTILE.CORP", 
"UNITED.SECURITY.FINL.CORP.IL", "EQUIVEST.FINANCE.INC", "I.V.C.I.CORP", 
"KINGS.ROAD.ENTERTAINMENT.INC", "SPORTING.LIFE.INC"), class = "data.frame")

问题是它返回66个资产但我只想要最好的4个满足参数。如何再次对其进行子集以获得所需的输出?

1 个答案:

答案 0 :(得分:1)

您可以使用top_n中的dplyr来选择&#39; n`顶行

library(dplyr)
top_n(AB, 4, BETAdn)

或者使用order中的base R,然后将顶部&#39; n&#39;行

AB[order(-AB$BETAdn),][1:4,]