1

我有一个 46175*741 矩阵。我想得到每一行的四分位数。基于这个问题(如何创建具有四分位数等级的列?),我尝试了:

dat_quartiles <- apply(t(dat) , 1, function(x) within(x, as.integer(cut(x, quantile(x, probs=0:4/4), include.lowest=TRUE))))

但我得到了错误:

Error in UseMethod("within") : no applicable method for 'within' applied to an object of class "c('integer', 'numeric')"

我到底哪里错了?

4

1 回答 1

4

我可能对您正在尝试做的事情阅读过多,但这应该是我能想到的用于返回矩阵中每一行的四分位数的最简单方法:

mat <- matrix(rnorm(1000), 100,10)
apply(mat, 1, quantile)

要分配分位数:

quantfun <- function(x) as.integer(cut(x, quantile(x, probs=0:4/4), include.lowest=TRUE))
apply(mat, 1, quantfun)
于 2013-07-11T12:55:20.487 回答