[R] apply cut function on various values (factor constrained)

Calle calle.scharffenorth at gmail.com
Mon Aug 3 13:14:40 CEST 2009

hi all,

presumably i have a small request, but i do indeed struggle. i want to
use the cut function as follows.

1) imagine two vectors X1 and X2
2) code values of X1 due to it's quantile values (as this is ordinary
use of the cut function)
3) code values of X2 due to it's quantile values, by taking the coded
values of X1 as factors (AND without having to loop)... most likely a
combination of tapply and cut...but

# data
# data
 X1 <- rnorm(n, mean=0.7, sd=10)
 X2 <- rnorm(n, mean=0.3, sd=25)

# code X1 values by using cut
X1_cut <- factor(cut(X1,quantile(X1),include.lowest=TRUE))

# calculate quantiles given X1_cut
X2QuantilesForX1_cut <-tapply(X2,X1_cut,quantile)

# display quantiles

# now i want to code X2 values; taking as breaks X2QuantilesForX1_cut;
code does not work, just for showing my intention
X2_cut <- cut(X1,X2QuantilesForX1_cut, include.lowest=TRUE)


any help is very much appreciated

- calle

More information about the R-help mailing list