get quartile for every row in matrix R -


i have 46175*741 matrix. want quartiles every row. based on question (how create column quartile rank?), tried :

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

but error:

error in usemethod("within") : no applicable method 'within' applied object of class "c('integer', 'numeric')" 

where going wrong?

i might reading trying do, owuld simplest way can think of returning quartiles of each row in matrix:

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

to assign number of quantile:

quantfun <- function(x) as.integer(cut(x, quantile(x, probs=0:4/4), include.lowest=true)) apply(mat, 1, quantfun) 

Comments