Dear R-Experts

I am sure this might look simple question for experts, at least is problem
for me. I have a large data frame with over 1000 variables and each have
different distribution( i.e. have different quantile). I want to create a
new grouped data frame, where the new variables where the value falling in
first (<25%), second (25% to <50%), third (50% to <75%) and fourth quantiles
(>75%) are replaced with 1,2,3, 4 respectively. The following example is
just to workout.
# my example:
   X1 <- c(1:10)

> X2 <- c(11:20)
> X3 <- c(21:30)
> X4 <- c(31:40)
> X5 <- c(41:50)
> dataf <- data.frame(X1, X2, X3, X4, X5)
>



> # my efforts of the last week led me to this point
>
for (i along(length(dataf[1,]))) {

> qntfun <- function (x) {
>   XQ <- as.numeric(as.matrix(quantile(x)))
>   Q1 <- XQ[1]
>   Q2 <- XQ[2]
>   Q3 <- XQ[3]
>   Q4 <- XQ[4]
>   for (i in 1:length(x)){
>   if (x[i] < Q2) {
>               x[i] <- 1
>               } else {
>               if ( x[i] > Q2 & x[i] < Q3){
>               x[i] <- 2
>               }   else {
>               if ( x[i] >Q3 & x[i] <Q4) {
>               x[i] <- 3
>               } else {
>               if (x[i] > Q4) {
>               x[i] <- 4
>               } else{
>                x[i] <- 0
>               }
>               }
>               }
>               }
>               }
>               }
> apply(dataf, 1:length(dataf), qntfun)
>  }
>
# I got error, I can not fix it. I would be glad to see a more slim
solution, but I could not think any.

Thanks in advance for your help.

Ram Sharma

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to