我有一个如下数据框
df<- structure(list(phenelzine = c(-0.0269, 0.0217, 0.4609, -0.0011,
0.0016, -0.0334, 0.5159, 0.4687, -0.5715, -0.1466, 0.1096, 0.1006,
-0.1021, 0.0093, -0.2616), denatonium.benzoate = c(-0.1734, -0.2142,
0.2142, -0.1376, -0.0129, 0.0254, 0.06, 0.1768, 0.2295, 0.1772,
0.1978, -0.0556, 0.0971, 0.0979, 0.2073), triamterene = c(-0.0253,
0.2896, 0.1606, -0.086, -0.1886, 0.0355, 0.0552, 0.0733, 0.0131,
0.6887, -0.0161, -0.3962, 0.1294, 0.0316, -0.1851), talampicillin = c(-0.1761,
-0.0355, 0.0295, 0.1979, 0.0545, 0.0673, -0.0797, -0.1566, 0.0496,
0.2623, -0.197, 0.0192, -0.2673, 0.0687, -0.0058), triamcinolone = c(0.0115,
0.3329, -0.0752, 0.2784, -0.5543, 0.0139, -0.6692, -0.4599, -4e-04,
0.0115, 0.0624, -0.2127, 0.1339, 0.1186, -0.0732), sulfaphenazole = c(-0.0886,
0.1081, 0.2002, 0.0625, 0.0403, 0.0256, -0.0074, -0.0678, -0.0393,
0.3201, 0.4213, -0.3058, -0.1228, 0.0797, 0.0591), procyclidine = c(0.03,
0.0372, 0.335, 0.1666, -0.0048, -0.1153, 0.1449, -0.0488, 0.1038,
-0.0245, 0.1008, 0.0194, -0.0315, -0.0325, 0.0161), pentoxifylline = c(0,
0.3439, -0.0614, 0.0181, -0.0149, -0.0216, -0.1211, -0.1816,
-0.0204, 0.1023, -0.0059, -0.008, -0.1121, 0.2029, 0.052), suloctidil = c(-0.0772,
-0.1651, -0.0543, -0.037, 0.2182, -0.1884, 0.1866, 0.2013, -0.3388,
0.0493, 0.0223, 0.3441, 0.0887, -0.1477, -0.0719), etacrynic.acid = c(0.1017,
-0.4238, -0.0089, -0.1116, 0.1265, -0.0529, -0.121, -0.0243,
-0.0033, -1e-04, 0.0952, 0.4075, -0.3078, 0.1265, -0.0612), diphenylpyraline = c(0.0495,
-0.1318, -0.0723, -0.0485, 0.0175, -0.1585, 0.0054, 0.0565, -0.0637,
0.0366, 0.0155, 0.1003, -0.11, -0.129, 0.0673), carbenoxolone = c(0.0769,
0.0987, 0.1992, -0.016, -0.0276, 0.2596, -0.0086, -0.0038, -0.2432,
-0.0237, 0.0696, -0.1436, 0.1942, -0.0411, -0.0948), arecoline = c(0.0185,
0.1999, -0.0313, -0.1868, -0.0626, 0.0298, 0.03, 0, -0.2209,
0.0101, -0.0693, -0.1656, -0.1048, 0.2098, 0.0393), chenodeoxycholic.acid = c(-0.0825,
0.2098, -0.0911, -0.0287, 0.0473, 0.1348, -0.06, -0.0285, -0.1473,
-0.0162, -0.0863, 0.0652, -0.0256, 0.1345, -0.1175), torasemide = c(0.0987,
0.1829, 0.1693, -0.0482, 0.0534, 0.1624, 0.0047, -0.1721, 0.248,
0.1715, -0.2109, -0.0909, 0.0513, -0.1358, 0.6297), troglitazone = c(-0.1513,
0.2568, 0.1377, 0.2474, -0.0359, -0.0859, 0.2014, 0.096, 0.0751,
0.1304, -0.1376, -0.1718, 0.0063, 0.2732, -0.2237), mepenzolate.bromide = c(0.0392,
-0.0041, -0.0531, -0.1213, -0.0919, 0.3096, 0.0545, 0.0922, 0.112,
0.0606, -0.25, -0.0086, 0.2729, 0.1307, 0.2124), megestrol = c(-0.1106,
0.2547, -0.0488, 0, -0.0406, -0.0826, -0.1795, -0.3099, -0.0316,
0.1563, 0, -0.1189, -0.0343, -0.0482, 0.8041), dexpropranolol = c(-0.1942,
-0.0051, 0.0561, 0.0166, 0.0029, -0.1707, 0.2173, 0.1178, -0.0683,
-0.2903, -0.2874, 0.1764, -0.1135, 0.2574, 0), nipecotic.acid = c(0.2653,
-0.3115, -0.0069, 0.118, -0.0998, -0.0289, 0.1051, 0.0132, -0.0028,
-0.1072, -0.1888, -0.0312, 0.3868, -0.0019, -0.1878), omeprazole = c(0.0272,
-0.3796, -0.0125, 0.0585, -0.1073, -0.0761, 0.3838, 0.5699, 0.0027,
-0.3822, 0.1132, -0.1588, 0.2396, -0.217, 0.0434), etanidazole = c(-0.1036,
0.0281, 0.0039, 0.0123, -0.2305, -0.0542, 0.0485, -0.0686, -0.1829,
0.2637, -0.1158, -0.2029, 0.104, 0.3169, 0.1387), acenocoumarol = c(-0.0087,
0.2005, -0.1051, -0.067, -0.0655, -0.0467, -0.4081, -0.1085,
-0.0556, 0.0145, -0.0865, 0.1961, -0.1842, -0.0398, -0.0944),
dacarbazine = c(-0.0065, -0.7082, 0.0979, 0.0048, -0.1409,
0.0541, -0.0793, -0.1102, -0.1513, -0.002, -0.0898, 0.0443,
0.0686, -0.1122, -0.0239), diphemanil.metilsulfate = c(-0.0312,
-0.2611, -0.0098, -0.216, 0.24, 0.0909, 0.1171, -0.156, 0.0986,
-0.0946, 0.1027, -0.1465, 0.0611, 0.0637, 0.0677), meprylcaine = c(-0.0382,
-0.1127, 0, 0, 0.0964, 0.0097, -0.0231, -0.0747, 0.2147,
0.0618, -0.2313, 0.0878, -0.3681, 0.0497, -0.0105), mevalolactone = c(-0.0393,
-0.174, -0.1632, 0.4181, -0.038, 0.1698, -0.1771, -0.168,
-0.3952, 0.1739, 0.1304, 0, -0.0317, -0.1822, -0.2021), buspirone = c(-0.1276,
-0.0097, -0.1365, 0.0801, -0.0499, 0.0499, 0.2245, 0.0739,
-0.0485, -0.0042, -0.2356, -0.1325, 0, 0, -0.0756), sulfafurazole = c(0.0336,
0.0083, 0.0309, 0.1891, -0.0991, 0.0259, 0.084, 0.0888, 0.0672,
-0.0859, -0.0196, -0.4085, 0.0676, 0.0357, -0.0486), ciclopirox = c(0.0958,
0.2534, -0.1897, -0.0457, 0.1704, -0.2632, -0.2596, -0.1076,
0.3008, 0.1348, -0.0817, 0.2929, -0.2249, -0.1357, -0.0498
)), .Names = c("phenelzine", "denatonium.benzoate", "triamterene",
"talampicillin", "triamcinolone", "sulfaphenazole", "procyclidine",
"pentoxifylline", "suloctidil", "etacrynic.acid", "diphenylpyraline",
"carbenoxolone", "arecoline", "chenodeoxycholic.acid", "torasemide",
"troglitazone", "mepenzolate.bromide", "megestrol", "dexpropranolol",
"nipecotic.acid", "omeprazole", "etanidazole", "acenocoumarol",
"dacarbazine", "diphemanil.metilsulfate", "meprylcaine", "mevalolactone",
"buspirone", "sulfafurazole", "ciclopirox"), row.names = c("200665_s_at",
"201125_s_at", "201150_s_at", "201162_at", "201560_at", "202133_at",
"202435_s_at", "202436_s_at", "202766_s_at", "203083_at", "203238_s_at",
"203640_at", "204223_at", "204468_s_at", "204589_at"), class = "data.frame")
我想要做的是,在我融化数据之后,我只保留那些 t&gt; 0.02 但不是全部,我希望能够说,最后10个,或者最后一个5或我喜欢的特定数字,这应适用于所有变量而不仅仅适用于一个变量
我所做的如下:
# sort the mean of the df
t<- data.frame(sort(rowMeans(df)))
# select only those that are higher than 0.02
rn <- rownames(t[t[,1] > 0.02,, drop=FALSE])
# melt the data frame
tab <- melt(transform(df, rowname = row.names(df)))
# select only those that are higher than 0.02
tab1 <- subset(tab, rowname %in% rownames(t)[t > 0.02])
答案 0 :(得分:2)
您可以使用tail
获取向量或data.frame的最后几行:
tail(tab1 ,5) ## the last five rows
tail(t,5) ## the last five conditions rows