【发布时间】:2016-04-25 07:19:13
【问题描述】:
到目前为止,我已经根据变量的值对所有内容进行了排序,例如,如果我有一排 n 数字,我会选择介于 a 和 b 之间的数字。我实际上需要做的是找到 %a 和 %b。
我一直在用这个:
a <- 05
b <- 0.4
colnames(data[,which(data > a & data < b)])
我需要的是把我的行分成十分位数。所以最高的 10% 值,然后是介于 10% - 20% 之间的值,依此类推,直到最高 90% -100%。值不能与十分位数重叠,并且我的数据不能完全除以 10。
编辑 我有以下数据块:
dput(data)
structure(list(AN8068571086 = c(0.501692168, 0.197414678, 0.415273482,
0.3078506, 0.36441391, 0.492483978, 0.398119861, 0.501925374,
0.660172121, 0.379188187), BMG3223R1088 = c(0.402426587, 0.214836776,
0.328226835, 0.265325336, 0.25724501, 0.396151915, 0.377199761,
0.31474308, 0.484177362, 0.412847814), BMG4388N1065 = c(0.592822703,
0.308105268, 0.374769701, 0.563959456, 0.335778936, 0.455266056,
0.510205508, 0.384208097, 0.460911179, 0.408350205), BMG6359F1032 = c(0.41153064,
0.221527294, 0.37383843, 0.329890556, 0.356333922, 0.397373547,
0.387519253, 0.424925141, 0.578383479, 0.411399158), BMG7496G1033 = c(0.478470376,
0.222667989, 0.33437412, 0.352835697, 0.299427154, 0.573123951,
0.466177145, 0.447775951, 0.477199807, 0.514107898), BMG812761002 = c(0.317522103,
0.265366064, 0.397487594, 0.348840651, 0.428338929, 0.282390173,
0.571658903, 0.450001013, 0.864445892, 0.418532333), CA88157K1012 = c(0.512859762,
0.183395043, 0.36847587, 0.364320833, 0.41197194, 0.628829565,
0.357019295, 0.341567448, 0.536733877, 0.343791549), CH0044328745 = c(0.499076264,
0.203778437, 0.310663532, 0.288884148, 0.247539664, 0.293768434,
0.348647329, 0.171457967, 0.391893463, 0.520079294), CH0048265513 = c(0.392308285,
0.245092722, 0.406807313, 0.338218477, 0.337216158, 0.396477472,
0.444780447, 0.513073443, 0.5655301, 0.372365682), GB00B4VLR192 = c(0.371059427,
0.243691452, 0.382559417, 0.36669396, 0.331187524, 0.336644629,
0.386660867, 0.408767967, 0.570252986, 0.350705351)), .Names = c("AN8068571086",
"BMG3223R1088", "BMG4388N1065", "BMG6359F1032", "BMG7496G1033",
"BMG812761002", "CA88157K1012", "CH0044328745", "CH0048265513",
"GB00B4VLR192"), row.names = c(NA, -10L), class = "data.frame")
该过程应按如下方式进行:(1) 跨行循环,(2) 找到最低 10% 的值,(3) 获取 10% 最低值所在的列的列名,并存储在一个列表中。下面的代码是我之前的代码,它搜索具有位于 a 和 b 之间的行值的列名。我需要的只是列名,而不是行中的实际值。
stockpicks <- list()
a <- 0.3
b <- 0.7
for (i in 1:nrow(data)) {
input <- as.matrix(data[1,])
#extract colnames of values between a and b
efficient <- matrix(colnames(data[,which(input > a & input < b)]))
# make a vector with new name for the output
tmp_date <- head(rownames(input), n=1)
#rename column
colnames(efficient) <-tmp_date
#export to list under new name
stockpicks[[tmp_date]] <- efficient
}
【问题讨论】:
-
你看过
?quantile吗?