[R] replace with quantile value for a large data frame...

From: Ram H. Sharma <sharma.ram.h_at_gmail.com>
Date: Sun, 13 Mar 2011 11:49:08 -0400


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_at_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. Received on Sun 13 Mar 2011 - 16:49:01 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 13 Mar 2011 - 17:10:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive